class hello {
public static void main(String[] args) throws IOException {
File one = Dir();
File two = Dir();
if(one.equals(two)) {
System.out.println("目标文件是源文件夹的子文件夹");
}else {
Copy(one,two);
}
}
public static void Copy(File one , File two) throws IOException {
File toDir = new File(two,one.getName());//获取目标文件夹的目录
toDir.mkdir();//在目标文件夹目录下新建与源文件夹相同名字的文件夹
File[] files = one.listFiles();
for (File file : files) {
if(file.isFile()) {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(toDir,file.getName())));
int a;
if((a = bis.read()) != -1){
bos.write(a);
}
bis.close();
bos.close();
}else {
Copy(file, toDir);
}
}
}
public static File Dir() {//获取文件夹路径方法
System.out.println("请输入文件夹路径:");
Scanner sc
利用File类和IO流将一个文件夹的所有内容拷贝到另一个文件夹下
最新推荐文章于 2022-07-19 10:17:03 发布