package day10_19.CopyFloder;
import java.io.*;
public class CopyFloders {
public static void main(String[] args) throws IOException {
File srcFile = new File("E:itcast");//得到源文件目录
File destFile = new File("D:");//输出目录
CopyFD(srcFile, destFile);
}
private static void CopyFD(File srcFile, File destFile) throws IOException {
if (srcFile.isDirectory()) {//如果源文件是目录
String srcFileName = srcFile.getName();//得到目录名
File newFloder = new File(destFile, srcFileName);
if (!newFloder.exists()) {
newFloder.mkdir();
}
File[] listFiles = srcFile.listFiles();
for (File file : listFiles) {
CopyFD(file, newFloder);
}
} else {
File newFile = new File(destFile, srcFile.getName());
Copy(srcFile, newFile);
}
}
private static void Copy(File srcFile, File destFile) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader(srcFile));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(destFile));
int len;
while ((len = bufferedReader.read()) != -1) {
bufferedWriter.write(len);
}
bufferedReader.close();
bufferedWriter.close();
}
}