package com.whn.practice;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Scanner;
public class Test8 {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要复制的文件夹: ");
String from = sc.nextLine();
File fromFile = new File(from);
System.out.println("请输入你要复制到哪个文件夹下: ");
String to = sc.nextLine();
File toFile = new File(to);
copy(fromFile,toFile);
System.out.println("复制完成");
}
/**
* 复制方法
* @param fromFile
* @param toFile
* @throws Exception
*/
private static void copy(File fromFile, File toFile) throws Exception {
File file = new File(toFile,fromFile.getName());
file.mkdirs();
File[] files = fromFile.listFiles();
for (File subFile : files) {
if (subFile.isDirectory()) {
copy(subFile, file);
}else{
//把文件的后缀改成".java",文件名在原文件名的后面加"(copy)"
String name = subFile.getName();
String[] names = name.split("\\.");
String firstName = names[0]+"copy" ;
String lastName = ".java";
String newName = firstName+lastName;
//复制文件
FileInputStream fis = new FileInputStream(subFile);
FileOutputStream fos = new FileOutputStream(new File(file,newName));
int a ;
while((a=fis.read()) !=-1){
fos.write(a);
}
fis.close();
fos.close();
}
}
}
}
|