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");
  }

}

结果如下:

  

原文地址:https://www.cnblogs.com/zhaosq/p/10950081.html