av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

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

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
相關文章:
主站蜘蛛池模板: 黄色一级免费 | 黄色大片在线 | 亚洲伊人影院 | 男男成人高潮片免费网站 | 国产视频在线观看视频 | 午夜免费在线 | 免费视频一区二区 | 黄色福利视频 | 一区二区三区四区视频在线观看 | 99热精品在线观看 | 国产精品久久久久久久久久久久久 | 日本熟妇毛耸耸xxxxxx | 国精产品99永久一区一区 | 欧洲黄色录像 | 婷婷一区二区三区 | 成人手机在线观看 | 亚洲色综合 | www.日本在线观看 | 久久久久久黄色 | 欧美视频免费看 | 国产91精品在线观看 | 精品网站999www | 中文字幕免费观看视频 | 一区二区三区视频在线播放 | 久久99精品久久久久久 | 色黄大色黄女片免费中国 | 夜夜草导航 | 法国极品成人h版 | 在线色综合 | 中文在线永久免费观看 | 中文字幕av一区二区三区谷原希美 | 中国少妇xxxxhd做受 | 亚洲国产一区二区三区 | 国产精品毛片一区视频播 | 日韩精品视频在线 | 国产精品av一区二区 | 羞羞的视频网站 | 亚洲欧美第一页 | 久久91视频 | 国产黄色免费看 | 日韩免费在线 |