溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2021-08-13 15:46:03 來(lái)源:億速云 閱讀:328 作者:小新 欄目:編程語(yǔ)言

小編給大家分享一下java如何將一個(gè)目錄下的所有數(shù)據(jù)復(fù)制到另一個(gè)目錄下,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

具體內(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 + "不存在,無(wú)法復(fù)制!");
      return;
    }else if((new File(TARGETSTRING)).exists()){
      System.out.println("目標(biāo)文件" + TARGETSTRING + "已經(jīng)存在,無(wú)法復(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() + "不可讀,無(wú)法復(fù)制!");
      return;
    }else{
      System.out.println("開(kāi)始復(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 + "不可讀,無(wú)法復(fù)制!");
      return;
    }else{
      (new File(targetPathString)).mkdirs();
      System.out.println("開(kāi)始復(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é)束");
    }
  }
}

以上是“java如何將一個(gè)目錄下的所有數(shù)據(jù)復(fù)制到另一個(gè)目錄下”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI