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/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流文件复制/解压的几种方法总结的更多相关文章
- 【Android】数据存储-java IO流文件存储
1.数据持久化:将在内存中的瞬时数据保存在存储设备中.瞬时数据:设备关机数据丢失.持久化技术提供一种机制可以让数据在瞬时状态和持久状态之间转换. 2.Android中简单的三种存储方式:文件存储.Sh ...
- java IO流文件的读写具体实例(转载)
引言: 关于java IO流的操作是非常常见的,基本上每个项目都会用到,每次遇到都是去网上找一找就行了,屡试不爽.上次突然一个同事问了我java文件的读取,我一下子就懵了第一反应就是去网上找,虽然也能 ...
- Java IO 流-- 文件拷贝
IO流操作套路: 1.创建源: 2.选择流: 3.操作: 4.释放资源 上代码: package com.xzlf.io; import java.io.File; import java.io.Fi ...
- linux下分卷压缩,合并解压的3种方法
我们上传东西的时候,由于文件过大而不能上传,或者不给上传,最明显的就是发邮件了,附件最大5M,有的10M.如果超过了就郁闷了.这个时候,如果能把压缩的东西,分割开来就比较爽了,windows下面我想大 ...
- Java io流完成复制粘贴功能
JAVA 中io字节输入输出流 完成复制粘贴功能: public static void main(String[] args) throws Exception{ // 创建输入流要读 ...
- java实现zip文件的解压
使用到的包 org.apache.commons 下载文件 url:文件所在地址需要是http:// filePath:将下载的文件保存的路径 public static void getDownlo ...
- java IO流文件的读写具体实例
IO流的分类:1.根据流的数据对象来分:高端流:所有的内存中的流都是高端流,比如:InputStreamReader 低端流:所有的外界设备中的流都是低端流,比如InputStream,Output ...
- Java—IO流 文件的编码
文件的编码 package cn.test; import java.io.UnsupportedEncodingException; public class Demo15 { public sta ...
- java IO流文件拷贝文件(字符流标准写法)
public static void copyFile2(String path1, String path2) { Reader reader = null; Writer writer = nul ...
随机推荐
- win10 ubuntu18双系统环境搭建
感谢前辈辛勤总结,根据这3篇文章成功配置了双系统 https://blog.csdn.net/qq_24624539/article/details/81775635 https://blog.csd ...
- php银联支付
简介 PHP银联支付 流程 1.注册 银联 - 技术开发平台和商户服务平台 https://open.unionpay.com 注意:注册时建议使用IE浏览器,之前注册时插件老是用不了,使用IE10以 ...
- 简述cookie ,localStrage,sessionStorage的区别?
1.cookie: 是一个回话跟踪技术,信息存储在用户硬盘,可以做全局变量. 什么是会话:用户进入网站,开始浏览到结束的这样的一个过程,称为一次会话. 会话跟踪技术:浏览器和服务器之间进行多次请求数据 ...
- 码书:编码与解码的战争 PDF 下载
码书:编码与解码的战争 PDF 下载 下载地址:https://pan.baidu.com/s/14Y_krHh-unOv4g2KYFFDgQ 如需分享码:[打开微信]->[扫描右侧二维码]-& ...
- Thesis Viva checklist
This list gives you suggestions helpful in preparing to defend your thesis: I know my thesis thoroug ...
- hdu2002 计算球体积【C++】
计算球体积 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- [BZOJ 3221][Codechef FEB13] Obserbing the tree树上询问
[BZOJ 3221]Obserbing the tree树上询问 题目 小N最近在做关于树的题.今天她想了这样一道题,给定一棵N个节点的树,节点按1~N编号,一开始每个节点上的权值都是0,接下来有M ...
- win7安装gmpy2
1.下载地址:https://pypi.python.org/pypi/gmpy2 2.安装python和pip python 安装 下载: https://www.python.org/getit/ ...
- Spring MVC-控制器(Controller)-可参数化视图控制器(Parameterizable View Controller )示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_parameterizableviewcontroller.htm 说明:示例基于 ...
- SpringMVC+security
转自:http://blog.csdn.net/bigshotzhang/article/details/12346979 下面我们将实现关于Spring Security3的一系列教程. 最终的目标 ...