引言

在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/abc.txt"));
    //copyFileUsingFileChannels(new File("E:/Ng/test/abc.txt"),new File("E:/Ng/test/abc.txt"));
    //copyFileUsingApacheCommonsIO(new File("E:/Ng/test/abc.txt"),new File("E:/Ng/test/abc.txt"));
    copyFileUsingJava7Files(new File("E:/Ng/test/abc.txt"),new File("E:/Ng/test/abc.txt"));
    //unZipFiles(new File("E:/Ng/test/nginx-1.12.2.zip"),"E:/Ng/test");
  }
}

结果如下:

Java IO流文件复制/解压的几种方法总结的更多相关文章

  1. 【Android】数据存储-java IO流文件存储

    1.数据持久化:将在内存中的瞬时数据保存在存储设备中.瞬时数据:设备关机数据丢失.持久化技术提供一种机制可以让数据在瞬时状态和持久状态之间转换. 2.Android中简单的三种存储方式:文件存储.Sh ...

  2. java IO流文件的读写具体实例(转载)

    引言: 关于java IO流的操作是非常常见的,基本上每个项目都会用到,每次遇到都是去网上找一找就行了,屡试不爽.上次突然一个同事问了我java文件的读取,我一下子就懵了第一反应就是去网上找,虽然也能 ...

  3. Java IO 流-- 文件拷贝

    IO流操作套路: 1.创建源: 2.选择流: 3.操作: 4.释放资源 上代码: package com.xzlf.io; import java.io.File; import java.io.Fi ...

  4. linux下分卷压缩,合并解压的3种方法

    我们上传东西的时候,由于文件过大而不能上传,或者不给上传,最明显的就是发邮件了,附件最大5M,有的10M.如果超过了就郁闷了.这个时候,如果能把压缩的东西,分割开来就比较爽了,windows下面我想大 ...

  5. Java io流完成复制粘贴功能

    JAVA 中io字节输入输出流 完成复制粘贴功能: public static void main(String[] args) throws Exception{        // 创建输入流要读 ...

  6. java实现zip文件的解压

    使用到的包 org.apache.commons 下载文件 url:文件所在地址需要是http:// filePath:将下载的文件保存的路径 public static void getDownlo ...

  7. java IO流文件的读写具体实例

    IO流的分类:1.根据流的数据对象来分:高端流:所有的内存中的流都是高端流,比如:InputStreamReader  低端流:所有的外界设备中的流都是低端流,比如InputStream,Output ...

  8. Java—IO流 文件的编码

    文件的编码 package cn.test; import java.io.UnsupportedEncodingException; public class Demo15 { public sta ...

  9. java IO流文件拷贝文件(字符流标准写法)

    public static void copyFile2(String path1, String path2) { Reader reader = null; Writer writer = nul ...

随机推荐

  1. JDK升级

    保存jboss运行时环境的配置 删除jboss下面的缓存文件 删除deployments里面的war包 重新build项目

  2. GridView中的日期处理

    数字 {0:N2} 12.36  数字 {0:N0} 13  货币 {0:c2} $12.36  货币 {0:c4} $12.3656  货币  "¥{0:N2}"  ¥12.36 ...

  3. return和return false的区别

    1. return返回null,起到中断方法执行的效果,只要不return false事件处理函数将会继续执行,表单将提交2. return false,事件处理函数会取消事件,不再继续向下执行.比如 ...

  4. Vi/Vim基本用法

    Vi/Vim是Linux中一款功能强大的编辑器,vi是Visual Interface的缩写,即可视化接口,vim是vi iMprove的缩写,即 vi的增强版(具有语法着色功能).它在Linux上的 ...

  5. noip模拟赛 时之终末

    题目背景 圣乔治:不用拘泥,剩下的时间已不多…… 圣乔治:直呼我的真名—— 丝佩碧雅:圣乔治大人 圣乔治:如今,已无法维持结界,或是抑制深渊的前进 圣乔治:既然如此,我将献上这副身躯,期望最后的战斗 ...

  6. 爬虫——response中获取的不带主域名的url的拼接

    scrapy中response提取的没有主域名的url拼接 # 1.导入urllib的parse # 2.调用parse.urljoin()进行拼接,例子中response.url会自动提取出当前页面 ...

  7. 大逃亡(escape.*)

    给出数字N(1<=N<=10000),X(1<=x<=1000),Y(1<=Y<=1000),代表有N个敌人分布一个X行Y列的矩阵上,矩形的行号从0到X-1,列号从 ...

  8. FORTIFY_SOURCE

    In recent years Linux distributions started treating security more seriously. Out of many security f ...

  9. 20180530利用Maxwell组件实时监听Mysql的binlog日志

    转自:https://blog.csdn.net/qq_30921461/article/details/78320750 http://kafka.apache.org/quickstart htt ...

  10. 算是学完了《Servlet&JSP学习笔记》,立此存照

    我感觉从构架上来说,算是入门了, 终于可以正式进入SPRING的学习啦...爽 代码就不弄了,真的太多了...花了差不多两周呢..