欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java文件快速copy复制实例代码

程序员文章站 2022-03-24 09:47:24
前言最近学习netty的时候发现nio包下有个filechannel类,经过了解这个类作用是个专门负责传输文件的通道,支持多线程,而且经过反复多次测试filechannel复制文件的速度比buffer...

前言

最近学习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流关闭方式不清楚的同学请看我这篇文章:

测试对比

复制目标文件:

Java文件快速copy复制实例代码

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,否则更慢

结果

Java文件快速copy复制实例代码

总结

到此这篇关于java文件快速copy复制的文章就介绍到这了,更多相关java文件快速copy复制内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: java copy 复制