星涛:采用java递归复制文件夹

时间:2021-01-13 08:57:10
package com.botao;

import java.io.*;

/**
* @author cbt28
*/
public class FileUtil {
public static String a="";
public static String b=""; public static void copyDir(File src, File target) throws IOException {
if (!target.exists()) {
target.mkdir();
}
File[] lf = src.listFiles();
for (File file : lf) {
if (file.isFile()) {
System.out.println(src + "\\" + file.getName() + "--->" + target + "\\" + file.getName());
boolean b = copyFile(new File(src + "\\" + file.getName()), new File(target + "\\" + file.getName()));
System.out.println(b);
} else {
copyDir(new File(src + "\\" + file.getName()), new File(target + "\\" + file.getName()));
}
}
} public static boolean copyFile(File src, File target) throws IOException {
FileReader fr = new FileReader(src);
BufferedReader br = new BufferedReader(fr); FileWriter fw = new FileWriter(target);
BufferedWriter bw = new BufferedWriter(fw);
String s = br.readLine();
while (s != null) {
bw.write(s);
bw.newLine();
s = br.readLine();
}
bw.close();
fw.close();
br.close();
fr.close();
return true;
}
}