package cn.edut.tarena.demo1;

import java.io.*;

import org.junit.Test;

public class Demo_copy {
	@Test
	public void test001() {
		copyAll("D:\\a", "D:\\b\\c\\d\\e\\f");
	}

	public void copyAll(String pathIn, String pathOut) {
		File fileIn = new File(pathIn) ;
		File fileOut = new File(pathOut) ;
		
		if(fileIn.isFile()) {
			try {
				copuFile(fileIn , fileOut) ;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(fileIn.isDirectory()){//如果是文件夹
			fileOut.mkdirs(); 
			File[] fs = fileIn.listFiles();
			for(File f : fs) {
				copyAll(f.getAbsolutePath(), pathOut+"\\"+f.getName());
			}
		}else {
			throw new RuntimeException();
		}
	}
	public void copuFile(File fileIn , File fileOut) throws IOException  {
		BufferedInputStream bis  = new BufferedInputStream(new FileInputStream(fileIn)) ;
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileOut)); 
		int b ;
		//批量读写
		byte[] bs  = new byte[1024*8] ; //一般,默认空间8M 
		while((b = bis.read(bs))!=-1) {
			bos.write(bs);
		}
		bos.flush();
		bis.close();
		bos.close();
	}
}