Java文件快速copy复制实例代码
作者:Java劝退师、 发布时间:2021-05-27 12:25:22
标签:java,copy,复制
前言
最近学习netty的时候发现nio包下有个FileChannel类,经过了解这个类作用是个专门负责传输文件的通道,支持多线程,而且经过反复多次测试FileChannel复制文件的速度比BufferedInputStream/BufferedOutputStream复制文件的速度快了近三分之一。在复制大文件的时候更加体现出FileChannel的速度优势。而且FileChannel是多并发线程安全的。代码也比较简洁
代码贴下
package com.niu.nio;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
/**
* @description:
* @author: nxq email: niuxiangqian163@163.com
* @createDate: 2020/12/28 5:48 下午
* @updateUser: nxq email: niuxiangqian163@163.com
* @updateDate: 2020/12/28 5:48 下午
* @updateRemark:
* @version: 1.0
**/
public class Main {
public static void main(String[] args) {
quickCopy(new File("/Users/laoniu/a.txt"),new File("/Users/laoniu/b.txt"));
}
/**
* 快速copy
* @author nxq
* @param src: 源文件
* @param target: 目标文件
* @return void
*/
public static void quickCopy(File src, File target){
try(FileInputStream inputStream = new FileInputStream(src);
FileOutputStream outputStream = new FileOutputStream(target);
FileChannel inputChannel = inputStream.getChannel(); // 得到源文件通道
FileChannel outputChannel = outputStream.getChannel()// 得到目标文件通道
) {
//将源文件数据通达连通到目标文件通道进行传输
inputChannel.transferTo(0,inputChannel.size(),outputChannel);
}catch (Exception e){
e.printStackTrace();
}
}
}
关于这种io流关闭方式不清楚的同学请看我这篇文章:https://www.jb51.net/article/203438.htm
测试对比
复制目标文件:
4.76GB
代码
package com.niu.nio;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
/**
* @description:
* @author: nxq email: niuxiangqian163@163.com
* @createDate: 2020/12/28 5:48 下午
* @updateUser: nxq email: niuxiangqian163@163.com
* @updateDate: 2020/12/28 5:48 下午
* @updateRemark:
* @version: 1.0
**/
public class Main {
public static void main(String[] args) {
long start = System.currentTimeMillis();
File src = new File("/Users/laoniu/Downloads/installer/cn_windows_10_business_edition_version_1809_updated_sept_2018_x64_dvd_fc5542c0.iso"); //文件4.76GB
quickCopy(src,new File("/Users/laoniu/test/a.iso"));
long end = System.currentTimeMillis();
System.out.println("FileChannel复制:"+(end - start));
start = System.currentTimeMillis();
copy(src,new File("/Users/laoniu/test/b.iso"));
end = System.currentTimeMillis();
System.out.println("普通复制:"+(end - start));
}
/**
* 快速copy
* @author nxq
* @param src: 源文件
* @param target: 目标文件
* @return void
*/
public static void quickCopy(File src, File target){
try(FileInputStream inputStream = new FileInputStream(src);
FileOutputStream outputStream = new FileOutputStream(target);
FileChannel inputChannel = inputStream.getChannel(); // 得到源文件文件通道
FileChannel outputChannel = outputStream.getChannel()// 得到目标文件通道
) {
//将源文件数据通达连通到目标文件通道进行传输
inputChannel.transferTo(0,inputChannel.size(),outputChannel);
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 普通copy
* @author nxq
* @param src:
* @param target:
* @return void
*/
public static void copy(File src, File target){
try(FileInputStream inputStream = new FileInputStream(src);
FileOutputStream outputStream = new FileOutputStream(target);
) {
byte[] data = new byte[1024*1024]; //加大每次读取的数据多少
int len;
while ((len = inputStream.read(data))!=-1){
outputStream.write(data,0,len);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
加大每次读取的数据到1024*1024,否则更慢
结果
总结
来源:https://blog.csdn.net/qq_41389354/article/details/111932720


猜你喜欢
- 前言EasyCache升级兼容 Springboot2,有个业务系统启动总是会卡住,最后抛出超时异常,如下:java.util.concur
- Java的IO是一个大知识点,如果把它的知识点拆开来说的话估计能说一个星期,关于IO的体系可以看看下面这张图,接下来我们从一段代码开始聊吧,
- Android的布局管理器本身就是个UI组件,所有的布局管理器都是ViewGroup的子类,而ViewGroup是View的子类,所以布局管
- 如:string str1 = "This is a test";string str2 = "This-is
- 1.通过无参构造函数创建(默认)2.通过有参构造创建1.constructor 的index赋值<bean id="user
- 首先需要明白一点,只有scop为(singleton)单例类型的Bean,spring才支持循环依赖。scope为(prototype)原型
- 1.什么是串口?在不会使用串口通讯之前,暂且可以把它理解为“一个可通讯的口”;使用篇不深入探讨理论及
- 本文实例讲述了C#查找字符串所有排列组合的方法。分享给大家供大家参考。具体实现方法如下:// 1. remove first char //
- 目录定义数据库表访问表中的数据插入数据查询数据创建数据库测试 DaoRoom 是 SQLite 的封装,它使 Android 对数据库的操作
- 一:本文使用范围此文不仅仅局限于spring boot,普通的spring工程,甚至是servlet工程,都是一样的,只不过配置一些 * 的
- 前言早就听说Go语言开发的服务不用任何架构优化,就可以轻松实现百万级别的qps。这得益于Go语言级别的协程的处理效率。协程不同于线程,线程是
- 一. MediaPlayer 状态机 介绍Android MediaPlayer 状态即图例 :1. Idle (闲置) 状态 和 End
- PrintStream 介绍PrintStream 是打印输出流,它继承于FilterOutputStream。PrintStream 是用
- 本文假设读者已经有一定Dagger2使用经验使用疑惑之前工作中一直在使用dagger2进行开发,用起来确实很爽,但是我从我第一次使用我就一直
- Spring Boot 默认为我们提供了静态资源处理,使用 WebMvcAutoConfiguration 中的配置各种属性。建议大家使用S
- 一、利用word生成一个文档转成pdf说明:转换成pdf格式二、Abobe Acrobat DC图解利用Abobe Acrobat DC打开
- @RequestBody出现400 Bad Request的问题今天与同事调试一个接口,发现后台使用@RequestBody老是获取不到数据
- 一. string的构造函数的形式:string str:生成空字符串string s(str):生成字符串为str的复制品string s
- 题外话:发现好久都没有上来写博文了,毕业设计加上公司暂时没有Android的项目做,只能去自学web上的知识,摸爬打滚到现在,花了一个多月时
- 计数排序是非比较的排序算法,用辅助数组对数组中出现的数字计数,元素转下标,下标转元素计数排序优缺点优点:快缺点:数据范围很大,比较稀疏,会导