Java檔案快速copy複製例項程式碼
阿新 • • 發佈:2021-01-06
前言
最近學習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
測試對比
複製目標檔案:
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,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,len); } }catch (Exception e){ e.printStackTrace(); } } }
加大每次讀取的資料到1024*1024,否則更慢
結果
總結
到此這篇關於Java檔案快速copy複製的文章就介紹到這了,更多相關Java檔案快速copy複製內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!