我目前正在处理从一个地方到另一个地方的文件夹。它工作正常,但它并没有复制所有其余文件和文件夹所在的原始文件夹。这是我正在使用的代码:
public static void copyFolder(File src, File dest) throws IOException {
if (src.isDirectory()) {
//if directory not exists, create it
if (!dest.exists()) {
dest.mkdir();
}
//list all the directory contents
String files[] = src.list();
for (String file : files) {
//construct the src and dest file structure
File srcFile = new File(src, file);
File destFile = new File(dest+"\\"+src.getName(), file);
//recursive copy
copyFolder(srcFile,destFile);
}
} else {
//if file, then copy it
//Use bytes stream to support all file types
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
//copy the file content in bytes
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.close();
System.out.println("File copied from " + src + " to " + dest);
}
}
所以我有文件夹 srcC:\test\mytest\..all folders..
我想把它复制到C:\test\myfiles
但不是让C:\test\myfiles\mytest\..all folders..
我得到C:\test\myfiles\..all folders..