package com.sdy.Test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class Test8 {
/**
* @throws IOException
* @从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)剪切到另一个文件夹中
*/
public static void main(String[] args) throws IOException {
String str1 = "输入需要拷贝的文件夹路径";
String str2 = "输入需要拷贝目标文件夹路径";
File dir1 = grtDir(str1);
File dir2 = grtDir(str2);
copy(dir1, dir2);
}
private static void copy(File dir1, File dir2) throws IOException {
File[] sudFiles = dir1.listFiles();
for (File subfile : sudFiles) {
if(subfile.isDirectory()){
new File(dir2,subfile.getName()).mkdir();
copy(subfile, new File(dir2,subfile.getName()));
}
else{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(subfile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(dir2, subfile.getName())));
int b;
if((b = bis.read()) != -1){
bos.write(b);
}
bis.close();
bos.close();
}
}
}
private static File grtDir(String str3) {
System.out.println(str3);
Scanner sc = new Scanner(System.in);
while (true) {
String str = sc.nextLine();
File file = new File(str);
if (!file.exists()) {
System.out.println("文件夹路径错误!");
continue;
} else if (file.isFile()) {
System.out.println("输入的是文件,不是文件夹!");
continue;
} else {
return file;
}
}
}
} |
|