在使用java进行开发时常常会用到文件和目录的增删复制等方法。我写了一个小工具类。和大家分享,希望大家指正:package com.wangpeng.utill;import java.io.File;import java.io.File
在使用java进行开发时常常会用到文件和目录的增删复制等方法。我写了一个小工具类。和大家分享,希望大家指正:
package com.wangpeng.utill;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.InputStream;import java.io.PrintWriter;public class ToolOfFile { public static void newFolder(String folderPath) throws Exception { try { java.io.File myFolder = new java.io.File(folderPath); if (!myFolder.exists()) { myFolder.mkdir(); } } catch (Exception e) { throw e; } } public static void newFile(String filePath) throws Exception { try { File myFile = new File(filePath); if (!myFile.exists()) { myFile.createNewFile(); } } catch (Exception e) { throw e; } } public static void newFile(String filePath, String fileContent) throws Exception { // 用来写入字符文件的便捷类 FileWriter fileWriter = null; // 向文本输出流打印对象的格式化表示形式,使用指定文件创建不具有自己主动行刷新的新 PrintWriter printWriter = null; try { File myFile = new File(filePath); if (!myFile.exists()) { myFile.createNewFile(); } fileWriter = new FileWriter(myFile); printWriter = new PrintWriter(fileWriter); printWriter.print(fileContent); printWriter.flush(); } catch (Exception e) { throw e; } finally { if (printWriter != null) { printWriter.close(); } if (fileWriter != null) { fileWriter.close(); } } } public static void copyFile(String oldPath, String newPath) throws Exception { InputStream inStream = null; FileOutputStream outStream = null; try { int byteread = 0; File oldfile = new File(oldPath); // 文件存在时 if (oldfile.exists()) { inStream = new FileInputStream(oldfile); outStream = new FileOutputStream(newPath); byte[] buffer = new byte[1444]; while ((byteread = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, byteread); } outStream.flush(); } } catch (Exception e) { throw e; } finally { if (outStream != null) { outStream.close(); } if (inStream != null) { inStream.close(); } } } public static void copyFile(InputStream inStream, String newPath) throws Exception { FileOutputStream outStream = null; try { int byteread = 0; outStream = new FileOutputStream(newPath); byte[] buffer = new byte[1444]; while ((byteread = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, byteread); } outStream.flush(); } catch (Exception e) { throw e; } finally { if (outStream != null) { outStream.close(); } if (inStream != null) { inStream.close(); } } } public static void copyFolder(String oldPath, String newPath) throws Exception { try { (new File(newPath)).mkdirs(); // 假设目录不存在 则建立新目录 File a = new File(oldPath); String[] file = a.list(); File tempIn = null; for (int i = 0; i < file.length; i++) { if (oldPath.endsWith(File.separator)) { tempIn = new File(oldPath + file[i]); } else { tempIn = new File(oldPath + File.separator + file[i]); } if (tempIn.isFile()) { copyFile(tempIn.getAbsolutePath(), newPath + "/" + (tempIn.getName()).toString()); } else if (tempIn.isDirectory()) {// 假设是子目录 copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]); } } } catch (Exception e) { throw e; } } public static void delFileX(String filePathAndName) { File myDelFile = new File(filePathAndName); myDelFile.delete(); } public static void delForder(String path) { File delDir = new File(path); if (!delDir.exists()) { return; } if (!delDir.isDirectory()) { return; } String[] tempList = delDir.list(); File temp = null; for (int i = 0; i < tempList.length; i++) { if (path.endsWith(File.separator)) { temp = new File(path + tempList[i]); } else { temp = new File(path + File.separator + tempList[i]); } if (temp.isFile()) { temp.delete(); } else if (temp.isDirectory()) { // 删除完里面全部内容 delForder(path + "/" + tempList[i]); } } delDir.delete(); } public static void main(String[] args) { String oldPath = "F:/test/aaa/"; String newPath = "F:/test/bbb/"; try { // ToolOfFile.newFolder("F:/test/hello/"); // ToolOfFile.newFile("F:/test/hello/world.txt","我爱你,the world!"); ToolOfFile.copyFolder(oldPath, newPath); // ToolOfFile.delForder("F:/test/hello"); } catch (Exception e) { e.printStackTrace(); } System.out.println("OK"); }}
--结束END--
本文标题: java文件和目录的增删复制
本文链接: https://lsjlt.com/news/225473.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0