import java.io.*;
public class CopyAll {
public void copyDir(File from, File to) {
if (!to.exists()) {
to.mkdirs();
}
File[] files = from.listFiles();
for (int i = 0; i < files.length; i++) {
File file1 = files[i];
File file2 = new File(to.getPath() + File.separator
+ files[i].getName());
if (!file1.isDirectory()) {
copyFile(file1, file2);
} else {
copyDir(file1, file2);
}
}
}
public void copyFile(File src, File dest) {
try {
System.out.println(src.getAbsoluteFile() + " -> "
+ dest.getAbsoluteFile());
FileInputStream in = new FileInputStream(src);
FileOutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
while (in.read(buffer) != -1) {
out.write(buffer);
}
out.close();
in.close();
System.out.println("文件拷贝成功");
} catch (Exception e) {
System.out.println("文件拷贝失败");
}
}
public static void main(String[] args) {
CopyAll t = new CopyAll();
t.copyDir(new File(args[0]), new File(args[1]));
}
}