源文件夹下所有的文件 复制到目标文件夹下(包括子文件夹)
public static void recursion(File srcFile,File destFile) throws IOException {
for (File file : srcFile.listFiles()) {
if(file.isFile()) {
//文件直接copy
copyFile(file.getPath(), destFile.getPath());
}
else if(file.isDirectory()) {
//继续递归
File nextFile = new File(destFile.getPath() + File.separator+ file.getName());
nextFile.mkdir();
recursion(file,nextFile);
}
}
}
单个文件复制
public static void copyFile(String srcPath, String destPath) throws IOException{
File file = new File(srcPath);
BufferedReader reader = null;
PrintWriter write = null;
try {
String strLine = "";
reader = new BufferedReader(new FileReader(file));
write = new PrintWriter(new File(destPath + File.separator + file.getName()));
while(( strLine = reader.readLine()) != null) {
write.println(strLine);
}
}
finally {
reader.close();
write.flush();
write.close();
}
}