读写效率
来源:互联网 发布:淘宝上耐克是正品吗 编辑:程序博客网 时间:2024/05/22 07:44
文件搜索器
代码块
拷贝文件
@requires_authorizationpublic static void copyFile(File srcFile, File destFile){ InputStream in = null; OutputStream out = null; try{ in = new FileInputStream(srcFile); out = new FileOutputStream(destFile); byte[] b = new byte[1024]; int length = in.read(b); //返回有效字节数 while(length != -1){ out.write(b, 0, length); length = in.read(b); }} catch(Exception e){ e.printStackTrace(); } finally{ try { in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } }
public class FileTool { // 拷贝 public static void copyFile(String srcPath, String destPath, String srcWord, String destWord) { copyFile(new File(srcPath), new File(destPath), srcWord, destWord); } // 拷贝文件 // 重载 用于将路径转化为文件 // 添加关键字转化功能 public static void copyFile(File srcFile, File destFile, String srcWord, String destWord) { InputStream in = null; OutputStream out = null; try { /* * 1.流 */ in = new FileInputStream(srcFile); out = new FileOutputStream(destFile); byte[] b = new byte[1024]; int length = in.read(b); // 返回有效字节数 /* * 关键字转化 拷贝 */ if (srcFile == null && "".equals(srcFile)) { // 不进行关键字转化 拷贝 while (length != -1) { out.write(b, 0, length); length = in.read(b); } } else {// 进行关键字转化 // 目标关键字为空则默认为"" if (destWord == null) { destWord = ""; } while (length != -1) { String string = new String(b, 0, length);// 获取0到length-1的字符串 string = string.replace(srcWord, destWord);// 关键字替换 out.write(b, 0, length); length = in.read(b); } } } catch (Exception e) { e.printStackTrace(); } finally { try { in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } } // 拷贝文件夹 public static void copyFolder(String srcPath, String destPath, String srcWord, String destWord) { copyFolder(new File(srcPath), new File(destPath), srcWord, destWord); } public static void copyFolder(File srcFile, File destFile, String srcWord, String destWord) { /** * 1.是否存在 */ if (srcFile != null && srcFile.exists()) { /** * 2.是否文件夹 */ if (srcFile.isDirectory()) { /** * 3.目的文件夹是否存在 */ // file流只能创建文件 不能创建文件夹 if (!destFile.exists()) { destFile.mkdirs(); } /** 4. 拿到文件夹里面的文件或文件夹的那个文件数组!!! **/ File[] files = srcFile.listFiles(); // 遍历递归 for (File f : files) { String string = f.getName(); File file = new File(destFile, string); copyFolder(f, file, srcWord, destWord); } } else {// 非文件夹则必定是文件 copyFile(srcFile, destFile, srcWord, destWord); } } } // 删除文件 public static void deleteFolder(String srcPath) { deleteFolder(new File(srcPath)); } public static void deleteFolder(File srcFile) { /** 1.判断是否存在 **/ if (srcFile.exists()) { /** 2.判断是否是文件夹 **/ if (srcFile.isDirectory()) { File[] files = srcFile.listFiles(); for (File f : files) { deleteFolder(f); } } srcFile.delete();// 文件 删除自己 } else { srcFile.delete(); } } public static void saveFile(String path, String content) { saveFile(new File(path), content); } public static void saveFile(File file, String content) { /** * 1.判断父类文件夹是否存在 */ if (!file.getParentFile().exists()) { file.mkdirs(); } /** * 2.写流 out */ OutputStream out = null; try { out = new FileOutputStream(file); out.write(content.getBytes()); } catch (IOException e) { e.printStackTrace(); } finally { try { if (out != null) out.close(); } catch (IOException e) { e.printStackTrace(); } finally { out = null; } } }}
带功能界面
package javaTEST;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JTextField;import javax.swing.filechooser.FileNameExtensionFilter;//增删查改界面public class FunctionUI extends JFrame { static String Path1;// 原地址 static String Path2;// 目的地址 static String Path3;// 删除文件 static String Path4;// 保存文件 public void showUI() { // 界面 setSize(515, 450); setLocationRelativeTo(null); setDefaultCloseOperation(3); setLayout(null); /* * 文件拷贝 */ // 标签 JLabel fileCopy = new JLabel("文件拷贝"); JLabel src = new JLabel("源路径"); JLabel dest = new JLabel("目标路径"); fileCopy.setBounds(10, 12, 70, 20); src.setBounds(10, 40, 70, 20); dest.setBounds(10, 65, 70, 20); // 源路径 JTextField srcPath = new JTextField(); srcPath.setEditable(true); srcPath.setBounds(80, 40, 365, 20); // 目标路径 JTextField destPath = new JTextField(); destPath.setEditable(true); destPath.setBounds(80, 65, 365, 20); // 选择按钮 JButton button1 = new JButton("..."); button1.setBounds(450, 40, 30, 20); JButton button2 = new JButton("..."); button2.setBounds(450, 65, 30, 20); // aaa转成bbb JTextField aaa = new JTextField(); JTextField bbb = new JTextField(); aaa.setBounds(10, 100, 200, 20); bbb.setBounds(280, 100, 200, 20); JLabel convertTo = new JLabel("转成"); convertTo.setBounds(230, 100, 60, 20); // 拷贝按钮 JButton copy = new JButton("确认拷贝"); copy.setBounds(195, 135, 100, 20); // 添加到界面 add(fileCopy); add(src); add(dest); add(srcPath); add(destPath); add(button1); add(button2); add(aaa); add(bbb); add(convertTo); add(copy); /* * 文件删除 */ // 标签 28 文件删除, 路径 JLabel fileDelete = new JLabel("文件删除"); fileDelete.setBounds(10, 172, 70, 20); JLabel path = new JLabel("路径"); path.setBounds(10, 200, 70, 20); // 文本框: 路径 JTextField route = new JTextField(); route.setEditable(true); route.setBounds(80, 200, 365, 20); // 按钮: 选择,删除 JButton button3 = new JButton("..."); button3.setBounds(450, 200, 30, 20); JButton delete = new JButton("删除"); delete.setBounds(195, 235, 100, 20); // 添加到界面 add(fileDelete); add(path); add(route); add(button3); add(delete); /* * 文件保存 */ // 标签: 文件保存, 目标路径 JLabel fileSave = new JLabel("文件保存"); fileSave.setBounds(10, 277, 70, 20); JLabel destSave = new JLabel("目标路径"); destSave.setBounds(10, 305, 70, 20); // 文本框: 目标路径, 输入框 JTextField savePath = new JTextField(); savePath.setBounds(80, 305, 365, 20); savePath.setEditable(true); JTextField enter = new JTextField(); enter.setBounds(10, 330, 415, 20); enter.setEditable(true); // 按钮: 选择, 保存 JButton button4 = new JButton("..."); button4.setBounds(450, 305, 30, 20); JButton save = new JButton("保存"); save.setBounds(430, 330, 60, 20); // 添加 add(fileSave); add(destSave); add(savePath); add(enter); add(button4); add(save); setVisible(true); button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser j = new JFileChooser(); j.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); j.showOpenDialog(null); // 设置后缀名 FileNameExtensionFilter filter = new FileNameExtensionFilter(".txt", "txt"); j.setFileFilter(filter); Path1 = j.getSelectedFile().getAbsolutePath(); srcPath.setText(Path1); } }); button2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser j = new JFileChooser(); j.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); j.showOpenDialog(null); Path2 = j.getSelectedFile().getAbsolutePath(); destPath.setText(Path2); } }); button3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser j = new JFileChooser(); j.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); j.showOpenDialog(null); Path3 = j.getSelectedFile().getAbsolutePath(); route.setText(Path3); } }); button4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser j = new JFileChooser(); j.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); j.showOpenDialog(null); Path4 = j.getSelectedFile().getAbsolutePath(); savePath.setText(Path4); } }); // 确认拷贝 copy.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String srcWord = aaa.getText(); String destWord = bbb.getText(); // FileTool ft = new FileTool(); FileTool.copyFile(Path1, Path2, srcWord, destWord); } }); // 删除 delete.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { FileTool ft = new FileTool(); Path3 = route.getText(); ft.deleteFolder(Path3); } }); // 保存 save.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { FileTool ft = new FileTool(); Path4 = savePath.getText(); String content = enter.getText(); ft.saveFile(Path4, content); } }); } public static void main(String args[]) { FunctionUI fc = new FunctionUI(); fc.showUI(); }}
0 0
- 读写效率
- 如何解决mysql读写效率
- 怎样提高游戏的文件读写效率
- 怎样提高游戏的文件读写效率
- io流使用缓冲区提高读写效率
- 读写文件,影响效率的因素
- 使用RINGCACHE提高CASSANDRA读写效率
- C#指针读写结构体,效率高
- 读写Array数组,不同方法的效率
- php读写文件与读写数据库的效率比较分享
- 磁盘读写和数据库读写哪个效率更高?磁盘读写与数据库的关系
- 磁盘读写和数据库读写哪个效率更高?磁盘读写与数据库的关系
- java中常见IO的读写效率对比
- Java中常见IO的读写效率对比
- SDK使用 Mapping File 提高文件读写效率
- C#在文件读写结构体 Marshal效率低
- 读写操作非常频繁的时候提高读的效率
- NIO和阻塞io的读写文件效率差异
- Python3.4连接MySQL
- Android WebView重定向GOBACK问题
- Leonard Cohen --- I am your Man 歌词意译
- 快速排序
- 应用setrlimit控制进程使用系统CPU
- 读写效率
- poj3617 Best Cow Line【贪心】
- Fast Food hdu 1227 dp动态规划
- 110. Balanced Binary Tree | 判断是否是平衡二叉树
- 网络中的MTU
- OpenCV3.0+MFC+VS2010打开图像
- 快速N次方练习
- mysql的用户管理
- FastDFS-分布式文件系统安装(单节点)