Java文件快速copy復(fù)制實(shí)例代碼
最近學(xué)習(xí)netty的時(shí)候發(fā)現(xiàn)nio包下有個(gè)FileChannel類,經(jīng)過了解這個(gè)類作用是個(gè)專門負(fù)責(zé)傳輸文件的通道,支持多線程,而且經(jīng)過反復(fù)多次測(cè)試FileChannel復(fù)制文件的速度比BufferedInputStream/BufferedOutputStream復(fù)制文件的速度快了近三分之一。在復(fù)制大文件的時(shí)候更加體現(xiàn)出FileChannel的速度優(yōu)勢(shì)。而且FileChannel是多并發(fā)線程安全的。代碼也比較簡(jiǎn)潔
代碼貼下
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: 目標(biāo)文件 * @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()// 得到目標(biāo)文件通道 ) { //將源文件數(shù)據(jù)通達(dá)連通到目標(biāo)文件通道進(jìn)行傳輸 inputChannel.transferTo(0,inputChannel.size(),outputChannel); }catch (Exception e){ e.printStackTrace(); } }}
關(guān)于這種io流關(guān)閉方式不清楚的同學(xué)請(qǐng)看我這篇文章:https://www.jb51.net/article/203438.htm
測(cè)試對(duì)比復(fù)制目標(biāo)文件:
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復(fù)制:'+(end - start)); start = System.currentTimeMillis(); copy(src,new File('/Users/laoniu/test/b.iso')); end = System.currentTimeMillis(); System.out.println('普通復(fù)制:'+(end - start)); } /** * 快速copy * @author nxq * @param src: 源文件 * @param target: 目標(biāo)文件 * @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()// 得到目標(biāo)文件通道 ) { //將源文件數(shù)據(jù)通達(dá)連通到目標(biāo)文件通道進(jìn)行傳輸 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]; //加大每次讀取的數(shù)據(jù)多少 int len; while ((len = inputStream.read(data))!=-1){ outputStream.write(data,0,len); } }catch (Exception e){ e.printStackTrace(); } } }
加大每次讀取的數(shù)據(jù)到1024*1024,否則更慢
結(jié)果
到此這篇關(guān)于Java文件快速copy復(fù)制的文章就介紹到這了,更多相關(guān)Java文件快速copy復(fù)制內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. python:刪除離群值操作(每一行為一類數(shù)據(jù))2. 使用ProcessBuilder調(diào)用外部命令,并返回大量結(jié)果3. 詳解Vue中Axios封裝API接口的思路及方法4. JSP實(shí)現(xiàn)客戶信息管理系統(tǒng)5. python中if嵌套命令實(shí)例講解6. python 批量下載bilibili視頻的gui程序7. python 通過exifread讀取照片信息8. 使用css實(shí)現(xiàn)全兼容tooltip提示框9. CSS自定義滾動(dòng)條樣式案例詳解10. python中HTMLParser模塊知識(shí)點(diǎn)總結(jié)
