java文件操作怎么样实现目录的复制,目录底层有文件又怎么复制?该循环来获得目录的路径,还是怎么样?求大神指点。。。
参考代码:
/**
* 复制一个目录及其子目录、文件到另外一个目录
* @param src
* @param dest
* @throws IOException
*/
private void copyFolder(File src, File dest) throws IOException {
if (src.isDirectory()) {
if (!dest.exists()) {
dest.mkdir();
}
String files[] = src.list();
for (String file : files) {
File srcFile = new File(src, file);
File destFile = new File(dest, file);
// 递归复制
copyFolder(srcFile, destFile);
}
} else {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
}
}
递归调用,一层一层的获取并复制文件
典型的递归调用应用场景。
http://lg-asus.iteye.com/blog/1585146
http://blog.csdn.net/elfylin/article/details/5917815
采纳的答案很给力,学习下。