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 ...
随机推荐
- Zabbix自带的mysql监控模块
Zabbix自带的mysql监控模块 [root@Cagios zabbix-]# cp conf/zabbix_agentd/userparameter_mysql.conf /usr/local/ ...
- iOS crash log 解析 symbol address = stack address - slide 运行时获取slide的api 利用dwarfdump从dsym文件中得到symbol
概述: 为什么 crash log 内 Exception Backtrace 部分的地址(stack address)不能从 dsym 文件中查出对应的代码? 因为 ASLR(Address spa ...
- js的replace, 高亮
";console.log(str.replace(/\,/g, "")); //输出 123 ";console.log(str);//输出123 " ...
- POJ1161——The Suspects
POJ1161——The Suspects The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 48 ...
- uva 1585 Score(Uva-1585)
vj:https://vjudge.net/problem/UVA-1585 不多说水题一个o一直加x就加的变为0 我的代码 #include <iostream> #include &l ...
- cmake编译安装mysql
运维开发技术交流群欢迎大家加入一起学习(QQ:722381733) 前言:这里我使用的安装方式是(cmake编译),我选择的版本是:cmake-2.8.8.tar.gz.mysql-5.5.32.ta ...
- [加强版] Codeforces 835D Palindromic characteristics (回文自动机、DP)
题目链接: https://codeforces.com/contest/835/problem/D 题意: 一个回文串是\(1\)-回文的,如果一个回文串的左半部分和右半部分一样且都是\(k\)-回 ...
- 并发通信Manage,队列, 互斥锁
目录 Manage 队列 先入先出 互斥锁 Manage 进程间的通信是被限制的 from multiprocessing import Process a = 1 def func(): glob ...
- 洛谷 P1972 BZOJ 1878 [SDOI2009]HH的项链
题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH 不断地收集新的贝壳,因此,他的项链变得越来越长. ...
- [bzoj1468][poj1741]Tree[点分治]
可以说是点分治第一题,之前那道的点分治只是模模糊糊,做完这道题感觉清楚了很多,点分治可以理解为每次树的重心(这样会把数分为若干棵子树,子树大小为log级别),然后统计包含重心的整个子树的值减去各个子树 ...