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();
}
}