色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

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

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

瀏覽:6日期: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
相關文章:
主站蜘蛛池模板: 最近手机中文字幕1 | 久久国产欧美日韩精品免费 | 亚洲成a人片在线观看 欧美 | 手机看片亚洲 | 国产精品高清免费网站 | 97操碰 | 亚洲精品一区二区三区福利 | 精品国产免费人成在线观看 | 成人免费视频社区 | 国产成人精品福利网站在线 | 激情欧美日韩一区二区 | 成人高清无遮挡免费视频软件 | 巨乳激情 | 亚洲欧美日本视频 | 欧美精品专区免费观看 | 日本一区二区免费在线观看 | 性做久久久久久久免费观看 | 香港台湾经典三级a视频 | 久草综合在线视频 | 91精品国产色综合久久不 | 欧美一区二区精品系列在线观看 | 欧美色综合高清视频在线 | 亚洲精选在线 | 中文字幕在线一区二区三区 | 国内精自线一二区 | 在线观看欧洲成人免费视频 | 高清午夜线观看免费 | 波多野结衣免费观看视频 | 国产亚洲欧美久久精品 | 国产日产欧美精品一区二区三区 | 久久免费视频在线 | 日本一道免费一区二区三区 | 色网站在线 | 国产盗摄视频 | 日本免费毛片 | 免费观看情趣v视频网站 | 国产亚洲一区二区三区不卡 | 日本综合久久 | 日韩美女爱爱 | 国产在线精品二区韩国演艺界 | 亚欧人成精品免费观看 |