java实现简单的目录复制

package Exercise;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class Three {
	public static void Copy(String A,String B) throws IOException {
		File file1 = new File(A);
		String[] files = file1.list();
		for(String name:files) {
			String t1 = A+"/"+name;
			File temp = new File(t1);
			if(temp.isDirectory()) {
				File newDir=new File(B+"/"+temp.getName());
		        newDir.mkdirs();//创建文件夹
		        String t2 = B+"/"+temp.getName();
			    Copy(t1,t2);
			}
			if(temp.isFile()) {
				FileInputStream in = new FileInputStream(temp);
				FileOutputStream out = new FileOutputStream(B+"/"+temp.getName());
				byte[] a = new byte[100];
				int n = -1;
				n = in.read(a);
				while(n !=-1) {
					out.write(a);
					n = in.read(a);
				}
				in.close();
				out.close();
				
			}
			
			
			
			
		}
	}
		

	
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入待复制文件夹路径:");
		String a = sc.next();//把a赋值到b;
		System.out.println("请输入您需要的目的地:");
		String b = sc.next();
		File temp = new File(a);
		File just = new File(b+"/"+temp.getName());//判断是否存在相同的目录
		if(just.exists()) {
			System.out.println("文件夹已存在,不能完成复制!");
		}
		else {			
			if(temp.exists()) {
				File newDir=new File(b+"/"+temp.getName());
			     newDir.mkdirs();//创建文件夹
				Copy(a,b+"/"+temp.getName());
				System.out.println("文件夹已完成复制!");
			}
			else {
				System.out.println("文件路径不存在!");
			}
			sc.close();
			
		}
		
		

	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值