黑马程序员技术交流社区
标题:
复制多层文件夹
[打印本页]
作者:
虎鹏
时间:
2015-5-16 23:38
标题:
复制多层文件夹
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
//"基础班第一次班会资料"---> Copy
public class BigTestDemo {
public static void main(String[] args) throws IOException {
File srcPath = new File("基础班第一次班会资料");
File destPath = new File("Copy");
copyMothed(srcPath,destPath);
}
private static void copyMothed(File srcPath,File destPath) throws IOException {
destPath.mkdir();
File[] file = srcPath.listFiles();
for (File file2 : file) {
if (file2.isDirectory()){
File dest = new File(destPath, file2.getName());
copyMothed(file2, dest);
}else {
File dest = new File(destPath,file2.getName());
copy(file2,dest);
}
}
}
private static void copy(File file2, File dest) throws IOException {
FileInputStream fis = new FileInputStream(file2);
FileOutputStream fos = new FileOutputStream(dest);
int ch = -1;
while((ch = fis.read()) != -1){
fos.write(ch);
}
fis.close();
fos.close();
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2