public class Test {
public static void main(String[] args) throws IOException {
File srcFile = new File("E:\\1课程\\Java_1");
File destFile = new File("E:\\1课程\\Java_2");
copyFileAndRename(srcFile, destFile);
System.out.println("完成");
}
public static void copyFileAndRename(File srcFile, File destFile) throws IOException {
if(srcFile.isFile() || srcFile.getName().endsWith(".txt")) {
FileInputStream in = new FileInputStream(srcFile);
File lastdestFile = new File(destFile.getAbsolutePath().replace(".txt",".java"));
FileOutputStream out = new FileOutputStream(lastdestFile);
byte[] b = new byte[1024];
int i = 0;
while((i = in.read(b)) != -1) {
out.write(b, 0, i);
}
in.close();
out.close();
System.out.println(srcFile.getName()+"完成");
}else {
File[] arrFile = srcFile.listFiles();
for (File newfile : arrFile) {
String newDestFile = destFile.getAbsolutePath()+"\\"+newfile.getName();
if(newfile.isDirectory()) {
new File(newDestFile).mkdirs();
}
copyFileAndRename(newfile,new File(newDestFile));
}
}
}
} |