Java IO流文件復制/解壓的幾種方法總結


引言

  在JavaWeb項目開發過程,涉及到IO文件的讀寫操作以及文件的復制copy操作是作為一個程序員不可獲取的知識,那接下來就總結一些copy文件的一些方法,與大家通過學習,如果還有其他更好的方法,歡迎大家留言探討.代碼如下:

package com.svse.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.nio.file.Files;

/***
*
*功能說明:復制文件 將FileA 復制為FileB文件
*@author:zsq
*create date:2019年5月30日 下午2:38:20
*修改人 修改時間 修改描述
*
*Copyright (c)2019北京智華天成科技有限公司-版權所有
*/
public class FileUtils {

  //(方法一)copy復制文件 將FileA 復制為FileB文件
  @SuppressWarnings("unused")
  private static void copyFileUsingFileStreams1(File source, File dest)
    throws IOException {
    InputStream input = null;
    OutputStream output = null;
    try {
      input = new FileInputStream(source);
      output = new FileOutputStream(dest);
      byte[] buf = new byte[1024];
      int bytesRead;
        while ((bytesRead = input.read(buf)) > 0) {
             output.write(buf, 0, bytesRead);
        }
    } finally {
      input.close();
      output.close();
    }

  }

  //(方法二)copy復制文件 將FileA 復制為FileB文件
  @SuppressWarnings("unused")
  private static void copyFileUsingFileChannels(File source, File dest) throws IOException {
    FileChannel inputChannel = null;
    FileChannel outputChannel = null;
    try {
      inputChannel = new FileInputStream(source).getChannel();
      outputChannel = new FileOutputStream(dest).getChannel();
      outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
    } finally {
      inputChannel.close();
      outputChannel.close();
    }

  }

  //(方法三)copy復制文件 將FileA 復制為FileB文件
  @SuppressWarnings("unused")
  private static void copyFileUsingApacheCommonsIO(File source, File dest)
     throws IOException {
      org.apache.commons.io.FileUtils.copyFile(source, dest);
   }

  //(方法四)copy復制文件 將FileA 復制為FileB文件
  @SuppressWarnings("unused")
  private static void copyFileUsingJava7Files(File source, File dest)
    throws IOException {
      Files.copy(source.toPath(), dest.toPath());
  }

   

  /**
  *
  *功能說明:將zip文件解壓到指定的目錄
  *輸入參數:zipFile待解壓的文件 descDir解壓到的目錄
  *輸出參數:
  *創建人:zsq
  *創建時間:2019年5月30日 下午3:04:16
  *
  */
  public static void unZipFiles(File zipFile, String descDir) throws IOException{
    ZipFile zip = new ZipFile(zipFile,Charset.forName("GBK"));//解決中文文件夾亂碼
    String name = zip.getName().substring(zip.getName().lastIndexOf('\\')+1, zip.getName().lastIndexOf('.'));
    File pathFile = new File(descDir+name);
    if (!pathFile.exists()) {
      pathFile.mkdirs();  //以給定的路徑加上待加壓文件的文件名創建文件夾
    }
    for (Enumeration<? extends ZipEntry> entries = zip.entries(); entries.hasMoreElements();) {
      ZipEntry entry = (ZipEntry) entries.nextElement();
      String zipEntryName = entry.getName();
      InputStream in = zip.getInputStream(entry);
      //String outPath = (descDir + name +"/"+ zipEntryName).replaceAll("\\*", "/");
      String outPath = (descDir +"/"+ zipEntryName).replaceAll("\\*", "/");
      // 判斷路徑是否存在,不存在則創建文件路徑
      File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
      if (!file.exists()) {
        file.mkdirs();
      }
      // 判斷文件全路徑是否為文件夾,如果是上面已經上傳,不需要解壓
      if (new File(outPath).isDirectory()) {
        continue;
      }
      // 輸出文件路徑信息
      System.out.println(outPath);
      FileOutputStream out = new FileOutputStream(outPath);
      byte[] buf1 = new byte[1024];

      int len;
      while ((len = in.read(buf1)) > 0) {
        out.write(buf1, 0, len);
      }

      IOUtils.closeQuietly(in);
      IOUtils.closeQuietly(out);
    }
     System.out.println("******************解壓完畢********************");
     return;
  }

  public static void main(String[] args) throws IOException {

    //copyFileUsingFileStreams1(new File("E:/Ng/test/abc.txt"),new File("E:/Ng/test/abc11.txt"));
    //copyFileUsingFileChannels(new File("E:/Ng/test/abc.txt"),new File("E:/Ng/test/abc22.txt"));
    //copyFileUsingApacheCommonsIO(new File("E:/Ng/test/abc.txt"),new File("E:/Ng/test/abc33.txt"));
    copyFileUsingJava7Files(new File("E:/Ng/test/abc.txt"),new File("E:/Ng/test/abc44.txt"));
    //unZipFiles(new File("E:/Ng/test/nginx-1.12.2.zip"),"E:/Ng/test");
  }

}

結果如下:

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM