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

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;
}
}
上一篇:Centos7.5搭建ELK-6.5.0日志分析平台


下一篇:java-FileUtils(复制文件夹、复制文件、字符串直接写入文件中)(新手)