java复制文件夹的代码实现

mac2024-05-11  32

import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class MyTest2 { public static void main(String[] args) { copyDir("D:\\Documents\\Downloads\\jieya", "D:\\"); } public static void copyDir(String fileAdress, String copyString) { File file = new File(fileAdress); if (file.isFile()) { try { copyFile(file.getAbsolutePath(), copyString+"\\"+file.getName()); } catch (IOException e) { e.printStackTrace(); } } else { File file1 = new File(copyString+"\\"+file.getName()); file1.mkdirs(); String[] list = file.list(); for (int i = 0; i < list.length; i++) { copyDir(file.getAbsolutePath() + "\\" + list[i], file1.getAbsolutePath() ); } } } //复制方法文件输入流 public static void copyFile(String oldString, String copyString) throws IOException { FileInputStream in = new FileInputStream(oldString); FileOutputStream out = new FileOutputStream(copyString); int len = 0; byte[] bytes = new byte[1024 * 8]; while ((len = in.read(bytes)) != -1) { out.write(len); } in.close(); out.close(); } }
最新回复(0)