亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

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

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

瀏覽:4日期: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: [email protected] * @createDate: 2020/12/28 5:48 下午 * @updateUser: nxq email: [email protected] * @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: [email protected] * @createDate: 2020/12/28 5:48 下午 * @updateUser: nxq email: [email protected] * @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
相關文章:
主站蜘蛛池模板: 日本美女a级片 | 亚洲欧美日韩成人 | 黑人巨大系列在线播放 | 西西午夜视频 | 高清一级毛片免免费看 | 久久99精品国产99久久6男男 | 女人被男人狂躁的免费视频 | 欧美成人黄色片 | 日韩欧美亚洲综合 | 97视频在线观看视频在线精品 | 成人爱爱爱欧美日本视频 | 91亚洲国产成人精品性色 | 色拍拍在线精品视频在线观看 | 亚洲成a人片在线观看播放 亚洲成a人片在线播放观看国产 | 精品久久久久不卡无毒 | 午夜精品一区二区三区免费视频 | 欧美夜夜精品一级爽 | 澳门一级特黄录像免费播黄 | 一线高清视频在线观看www国产 | 免费观看又色又爽又大高清网站 | 丝袜美腿国产精品视频一区 | 一区二区福利视频 | 国产精品尹人在线观看免费 | 亚洲精品高清国产一久久 | 国产成人精品日本亚洲语音1 | 欧美日韩在线视频专区免费 | 亚洲黄网在线观看 | 成人国产免费 | 制服丝袜 自拍偷拍 | 天天色综 | 99久久国语露脸精品国产 | 丁香婷婷六月 | 欧美最黄视频 | 亚洲欧美日韩另类精品一区二区三区 | 精品国产欧美一区二区五十路 | 成人免费视频观看无遮挡 | 久久久精品久久久久久 | 美女三级黄| 请以你的名字呼唤我免费观看 | 欧美亚洲综合另类在线观看 | 久久久99精品免费观看 |