java 简单的拷贝文件夹

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

class Wenjian implements Runnable{
	static String str="C:\Users\cdlx2016\Desktop\实验";
	static String str10="C:\Users\cdlx2016\Desktop\实验2";

	static int a=0;
	 //static File file = new File(str);
      @Override
    public void run() {
          synchronized ("222") {
        	  
        	  paly(str10);
          }
    }
      public void paly(String str11){
    
    	  File file1 = new File(str11);
    	  File file = new File(str);
    	  //创建新的文件夹
    	  file1.mkdirs();
			File[] files = file.listFiles();
			for (int i = 0;i<files.length;i++) {
				//System.out.println(files[i].getName());
				if(files[i].isFile()){
					//拼接新的文件地址
					String s = str11+"\"+files[i].getName();
					//原来的文件地址
					String s1 = files[i].getPath();
						ShuRu(s1,s);
				}
				if (files[i].isDirectory()) {
					
					str=files[i].getPath();
					//拼接新的文件夹地址
					str10=str11+"\"+files[i].getName();
					
					paly(str10);
			}
	   }
    	  
      }
      public static void ShuRu(String str4,String s) {
    		File file =new File(str4);
    			
    			FileInputStream iputFileInputStream=null;
    			try {
    				iputFileInputStream = new FileInputStream(file);
    				byte[] b = new byte[1024];
    				int cuont=0;
    				try {
    					while ((cuont=iputFileInputStream.read(b))!=-1) {
    						System.out.println(new String(b, 0, cuont));
    						ShuChu(b,s);	
    					}
    				} catch (IOException e) {
    					 System.out.println("你的硬盘有问题");
    					 throw new RuntimeException(e);
    				}
    			} catch (FileNotFoundException e) {
    				 System.out.println("你写入的文件有误");
    			       throw new RuntimeException(e);
    			}finally{
    				
    				try {
    					iputFileInputStream.close();
    				} catch (IOException e) {
    					 System.out.println("无法关闭");
    					 throw new RuntimeException(e);
    				}
    				
    			}
    			
    		} 
      public static void ShuChu(byte[] bb ,String s){
  		//C:Userscdlx2016Desktop
    	  a++;
  		File file =new File(s);
  		FileOutputStream outputStream = null;
  		try {
  			outputStream = new FileOutputStream(file,true);
  			byte [] b = bb;
  			try {
  				outputStream.write(b);
  			} catch (IOException e) {
  				 System.out.println("你的硬盘有问题");
  				 throw new RuntimeException(e);
  			}
  		} catch (FileNotFoundException e) {
  		       System.out.println("你写出的文件有误");
  		       throw new RuntimeException(e);
  		}finally{
  			try {
  				outputStream.close();
  			} catch (IOException e) {
  				 System.out.println("无法关闭");
  				 throw new RuntimeException(e);
  				 
  			}
  		}
  		
  		
  	}
}



public class Work1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
    
//(七)输入两个文件夹名称,将A文件夹内容全部拷贝到B文件夹,要求使用多线程来操作。 必做
		Wenjian ww=new Wenjian();
		  Thread t = new Thread(ww,"下载一");
		  Thread t1 = new Thread(ww,"下载一");
		  t.start();
		  t1.start();
		 
		  
		
	}

}

 

原文地址:https://www.cnblogs.com/zzzhangzheng/p/6137016.html