欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java將一個目錄下的所有數(shù)據(jù)復(fù)制到另一個目錄下

 更新時間:2020年11月30日 17:10:56   作者:wangtianze  
這篇文章主要為大家詳細介紹了java將一個目錄下的所有數(shù)據(jù)復(fù)制到另一個目錄下,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java將一個目錄下的所有數(shù)據(jù)復(fù)制到另一個目錄下的具體代碼,供大家參考,具體內(nèi)容如下

/*
 將"C:\\JavaProducts\\Source"下的所有數(shù)據(jù)復(fù)制到"C:\\Target"下
*/

import java.io.*;

public class JavaCopyDemo{
 final static String SOURCESTRING = "C:\\JavaProducts\\Source";
 final static String TARGETSTRING = "C:\\Target";
 
 public static void main(String[] args){
  if(!(new File(SOURCESTRING)).exists()){
   System.out.println("源文件" + SOURCESTRING + "不存在,無法復(fù)制!");
   return;
  }else if((new File(TARGETSTRING)).exists()){
   System.out.println("目標文件" + TARGETSTRING + "已經(jīng)存在,無法復(fù)制!");
   return;
  }else{
   if((new File(SOURCESTRING)).isFile()){
    copyFile(new File(SOURCESTRING),new File(TARGETSTRING));
   }else if((new File(SOURCESTRING)).isDirectory()){
    copyDirectory(SOURCESTRING,TARGETSTRING);
   }
  }
 }
 
 private static void copyFile(File sourceFile,File targetFile){
  if(!sourceFile.canRead()){
   System.out.println("源文件" + sourceFile.getAbsolutePath() + "不可讀,無法復(fù)制!");
   return;
  }else{
   System.out.println("開始復(fù)制文件" + sourceFile.getAbsolutePath() + "到" + targetFile.getAbsolutePath());
   FileInputStream fis = null;
   BufferedInputStream bis = null;
   FileOutputStream fos = null;
   BufferedOutputStream bos = null;
   
   try{
    fis = new FileInputStream(sourceFile);
    bis = new BufferedInputStream(fis);
    fos = new FileOutputStream(targetFile);
    bos = new BufferedOutputStream(fos);
    int len = 0;
    while((len = bis.read()) != -1){
     bos.write(len); 
    }
    bos.flush();
    
   }catch(FileNotFoundException e){
    e.printStackTrace(); 
   }catch(IOException e){
    e.printStackTrace();
   }finally{
    try{
     if(fis != null){
      fis.close(); 
     }
     if(bis != null){
      bis.close(); 
     }
     if(fos != null){
      fos.close(); 
     }
     if(bos != null){
      bos.close(); 
     }
     System.out.println("文件" + sourceFile.getAbsolutePath() + "復(fù)制到" + targetFile.getAbsolutePath() + "完成");
    }catch(IOException e){
     e.printStackTrace();
    }
   }
  }
 }
 
 private static void copyDirectory(String sourcePathString,String targetPathString){
  if(!new File(sourcePathString).canRead()){
   System.out.println("源文件夾" + sourcePathString + "不可讀,無法復(fù)制!");
   return;
  }else{
   (new File(targetPathString)).mkdirs();
   System.out.println("開始復(fù)制文件夾" + sourcePathString + "到" + targetPathString);
   File[] files = new File(sourcePathString).listFiles();
   for(int i = 0; i < files.length; i++){
    if(files[i].isFile()){
     copyFile(new File(sourcePathString + File.separator + files[i].getName()),new File(targetPathString + File.separator + files[i].getName())); 
    }else if(files[i].isDirectory()){
     copyDirectory(sourcePathString + File.separator + files[i].getName(),targetPathString + File.separator + files[i].getName());
    } 
   }
   System.out.println("復(fù)制文件夾" + sourcePathString + "到" + targetPathString + "結(jié)束");
  }
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論