求解答!
public class Test1 {
public static void main(String[] args) throws Exception {
System.out.println("请输入目标文件夹路径:");
File dest = getDir();
System.out.println("请输入源文件路径:");
File src = getDir();
if (dest.equals(src)){
System.out.println("您输入的文件夹路径相同");
}
else {
copy(dest,src);
}
}
//复制文件
public static void copy(File dest,File src) throws IOException{
File newDir = new File(dest,src.getName());
newDir.mkdir();
File[] subFiles = src.listFiles();
for (File subFile : subFiles) {
if (subFile.isFile() && subFile.getName().endsWith(".java")){
FileInputStream fis = new FileInputStream(subFile);
FileOutputStream fos = new FileOutputStream(new File(newDir,subFile.getName()));
byte[] array = new byte[1024];
int len;
while((len = fis.read(array)) != -1){
fos.write(array,0,len);
}
fis.close();
fos.close();
}
else {
copy(newDir,subFile);
}
}
}
//从控制台接收文件夹
public static File getDir() {
Scanner sc = new Scanner(System.in);
while(true){
String s1 = sc.nextLine();
File file = new File(s1);
if(!file.exists()){
System.out.println("您输入的文件夹路径不存在,请重新输入:");
}
else if (file.isFile()){
System.out.println("您输入的是一个文件路径,请重新输入:");
}
else {
return file;
}
}
}
} |
|