public static void copyDir(File src, File dest) throws FileNotFoundException{
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);
copyDir(srcFile, destFile);
}
}
else{
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
try {
while((length=in.read(buffer))!=-1){
out.write(buffer, 0, length);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
主函数
public static void main(String[] args) {
String source = "E:\\aa";
String dest = "D:\\dd";
File src = new File(source);
File des = new File(dest);
try {
FileUtils.copyDir(src, des);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
复制目录 只能复制aa文件夹以下的文件夹和文件,怎么改?可以把aa也一起复制呢?