zoukankan      html  css  js  c++  java
  • 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");
      }

    }

    结果如下:

      

  • 相关阅读:
    idea 快捷键ctrl+shift+f失效的解决方案
    Nacos2.0启动遇到的问题以及解决方法(自测有效)
    Nacos客户端升级到2.x版本后,启动出现9848端口错误解决
    用友Co 与直接插库速度比较
    CSS text-decoration_下划线
    uni-app组件之image
    JavaScript :Array数组之filter()——检查指定数组符合条件的所有元素
    爬虫与Python:(三)基本库的使用——2.网络请求库之request安装
    爬虫与Python:(三)基本库的使用——1.网络请求库之urllib()
    Python报错:TypeError: string argument without an encoding
  • 原文地址:https://www.cnblogs.com/zhaosq/p/10950081.html
Copyright © 2011-2022 走看看