public static void copy(File src, File dest) throws IOException {
//1,在目标文件夹中创建原文件夹
File newDir = new File(dest, src.getName());
newDir.mkdir();
//2,获取原文件夹中所有的文件和文件夹,存储在File数组中
File[] subFiles = src.listFiles();
//3,遍历数组
for (File subFile : subFiles) {
//4,如果是文件就用io流读写
if(subFile.isFile()) {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(subFile));
BufferedOutputStream bos =
new BufferedOutputStream(new FileOutputStream(new File(newDir,subFile.getName())));
int b;
while((b = bis.read()) != -1) {
bos.write(b);
}
bis.close();
bos.close();
//5,如果是文件夹就递归调用
}else {
copy(subFile,newDir);
}
}
} |
|