我坚信:talk is cheap, show me the code.
public class CopyFileTest {
public static void main(String[] args) throws IOException {
File f_1 = new File("D:\\IDEA_DarkHorse\\JavaJob\\MyTest\\src\\com\\leboy1\\aaa");
File f_2 = new File("D:\\IDEA_DarkHorse\\JavaJob\\job_day15\\src\\com\\leboy\\exer3");
copyFile(f_1,f_2);
}
private static void copyFile(File from,File to) throws IOException {
File[] files = from.listFiles();
File f_to = new File(to, from.getName());
f_to.mkdir();
if (files.length == 0 ){
return;
}
to = f_to;
for (File f : files) {
if (f.isDirectory()){
File dic = new File(to, f.getName());
dic.mkdir();
copyFile(f,dic);
}else {
byte[] bytes = new byte[1024];
int len = 0 ;
FileInputStream f_in = new FileInputStream(f);
FileOutputStream f_out = new FileOutputStream(new File(to, f.getName()));
while ((len = f_in.read(bytes))!=-1){
f_out.write(bytes,0 ,len );
}
f_out.close();
f_in.close();
}
}
}
}