久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

Java文件快速copy復制實例代碼

瀏覽:3日期:2022-08-18 17:42:48
前言

最近學習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

測試對比

復制目標文件:

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
相關文章:
主站蜘蛛池模板: 许昌县| 河津市| 西乌珠穆沁旗| 曲水县| 乐平市| 湄潭县| 平罗县| 巴林左旗| 阳高县| 深水埗区| 陈巴尔虎旗| 阳谷县| 乐亭县| 灵武市| 慈溪市| 攀枝花市| 湘潭县| 手游| 桓台县| 民丰县| 牙克石市| 客服| 平陆县| 泰来县| 龙游县| 申扎县| 玉树县| 景泰县| 曲阜市| 渝中区| 策勒县| 云南省| 武隆县| 广德县| 青岛市| 二连浩特市| 洪洞县| 曲松县| 英德市| 康平县| 纳雍县|