复制目录和目录下的所有文件

package cn.hmm.Copy;

import java.io.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class newCopy {


	public static void main(String[] args) throws IOException {
		// TODO 自动生成的方法存根
		File f1 = new File("C:/abc");
		long startime = 0;
		System.out.println(startime = System.currentTimeMillis());
		find(f1);
		System.out.println(System.currentTimeMillis()-startime+"毫秒");
		/*File f2 = new File("d:\\2.txt");
		
		InputStream in = new FileInputStream(f1);
		OutputStream out = new FileOutputStream(f2);
		copy(in,out);
		in.close();
		out.close();*/
	}
	
	private synchronized static void find(final File f1){
		File f2 = null;
		if(f1.isDirectory()){
			String Path = f1.getAbsolutePath();
			System.out.println(Path);
			String regex = "^[a-z]|[A-Z]{1}:{1}";
			String[] Path1 = Path.split(regex);
			Path = "D:"+Path1[1];
			f2 = new File(Path);
			System.out.println(f2.mkdirs());
			System.out.println(Path);
			File[] fs = f1.listFiles();
			for(File f:fs){
				find(f);
			}
		}
		else if(f1.isFile()){
			
				
					// TODO 自动生成的方法存根
					System.out.println(Thread.currentThread().toString());
					copyfile(f1);
		
		}
			
	}

	private static void copyfile(File f1) {
		File f2;
		String Path = f1.getAbsolutePath();
		System.out.println(Path);
		String regex = "^[a-z]|[A-Z]{1}:{1}";
		String[] Path1 = Path.split(regex);
		Path = "D:"+Path1[1];
		f2 = new File(Path);
		InputStream in;
		try {
			in = new FileInputStream(f1);
			OutputStream out = new FileOutputStream(f2);
			copy(in,out);
			in.close();
			out.close();
		} catch (FileNotFoundException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}
	private static void copy(final InputStream ips,final OutputStream out) {
	
				int len = 0;
				byte[] bytes = new byte[1024];
			try {
				while((len=ips.read(bytes))!=-1){
					out.write(bytes);
				}
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
					
	
		
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值