system文件_大文件拷贝,试试NIO的内存映射
最近項(xiàng)目里有個需求需要實(shí)現(xiàn)文件拷貝,在java中文件拷貝流的讀寫,很容易就想到IO中的InputStream和OutputStream之類的,但是上網(wǎng)查了一下文件拷貝也是有很多種方法的,除了IO,還有NIO、Apache提供的工具類、JDK自帶的文件拷貝方法
IO拷貝
public class IOFileCopy { private static final int BUFFER_SIZE = 1024; public static void copyFile(String source, String target) { long start = System.currentTimeMillis(); try(InputStream in = new FileInputStream(new File(source)); OutputStream out = new FileOutputStream(new File(target))) { byte[] buffer = new byte[BUFFER_SIZE]; int len; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } System.out.println(String.format("IO file copy cost %d msc", System.currentTimeMillis() - start)); } catch (Exception e) { e.printStackTrace(); } }}傳統(tǒng)IO中文件讀取過程可以分為以下幾步:
- 內(nèi)核從磁盤讀取數(shù)據(jù)到緩沖區(qū),這個過程由磁盤操作器通過DMA操作將數(shù)據(jù)從磁盤讀取到內(nèi)核緩沖區(qū),該過程不依賴CPU
- 用戶進(jìn)程在將數(shù)據(jù)從內(nèi)核緩沖區(qū)拷貝到用戶空間緩沖區(qū)
- 用戶進(jìn)程從用戶空間緩沖區(qū)讀取數(shù)據(jù)
NIO拷貝
NIO進(jìn)行文件拷貝有兩種實(shí)現(xiàn)方式,一是通過管道,而是通過文件內(nèi)存內(nèi)存映射
public class NIOFileCopy { public static void copyFile(String source, String target) { long start = System.currentTimeMillis(); try(FileChannel input = new FileInputStream(new File(source)).getChannel(); FileChannel output = new FileOutputStream(new File(target)).getChannel()) { output.transferFrom(input, 0, input.size()); } catch (Exception e) { e.printStackTrace(); } System.out.println(String.format("NIO file copy cost %d msc", System.currentTimeMillis() - start)); }}文件內(nèi)存映射:
把內(nèi)核空間地址與用戶空間的虛擬地址映射到同一個物理地址,DMA 硬件可以填充對內(nèi)核與用戶空間進(jìn)程同時可見的緩沖區(qū)了。用戶進(jìn)程直接從內(nèi)存中讀取文件內(nèi)容,應(yīng)用只需要和內(nèi)存打交道,不需要進(jìn)行緩沖區(qū)來回拷貝,大大提高了IO拷貝的效率。加載內(nèi)存映射文件所使用的內(nèi)存在Java堆區(qū)之外
public class NIOFileCopy2 { public static void copyFile(String source, String target) { long start = System.currentTimeMillis(); try(FileInputStream fis = new FileInputStream(new File(source)); FileOutputStream fos = new FileOutputStream(new File(target))) { FileChannel sourceChannel = fis.getChannel(); FileChannel targetChannel = fos.getChannel(); MappedByteBuffer mappedByteBuffer = sourceChannel.map(FileChannel.MapMode.READ_ONLY, 0, sourceChannel.size()); targetChannel.write(mappedByteBuffer); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println(String.format("NIO memory reflect file copy cost %d msc", System.currentTimeMillis() - start)); File targetFile = new File(target); targetFile.delete(); }}NIO內(nèi)存映射文件拷貝可以分為以下幾步
NIO的內(nèi)存映射實(shí)際上就是少了一次從內(nèi)核空間拷貝到用戶空間的過程,將對用戶緩沖區(qū)的讀改為從內(nèi)存讀取
Files#copyFile方法
public class FilesCopy { public static void copyFile(String source, String target) { long start = System.currentTimeMillis(); try { File sourceFile = new File(source); File targetFile = new File(target); Files.copy(sourceFile.toPath(), targetFile.toPath()); } catch (IOException e) { e.printStackTrace(); } System.out.println(String.format("FileCopy file copy cost %d msc", System.currentTimeMillis() - start)); }}FileUtils#copyFile方法
使用FileUtils之前需先引入依賴
- 依賴 commons-io commons-io 2.4
- FileUtils#copyFile封裝類:FileUtilsCopy.javapublic class FileUtilsCopy { public static void copyFile(String source, String target) { long start = System.currentTimeMillis(); try { FileUtils.copyFile(new File(source), new File(target)); } catch (IOException e) { e.printStackTrace(); } System.out.println(String.format("FileUtils file copy cost %d msc", System.currentTimeMillis() - start)); } }
性能比較
既然有這么多種實(shí)現(xiàn)方法,肯定要從中選擇性能最佳的
測試環(huán)境:
- windows 10
- CPU 6核
- JDK1.8
測試代碼:PerformTest.java
public class PerformTest { private static final String source1 = "input/test1.txt"; private static final String source2 = "input/test2.txt"; private static final String source3 = "input/test3.txt"; private static final String source4 = "input/test4.txt"; private static final String target1 = "output/test1.txt"; private static final String target2 = "output/test2.txt"; private static final String target3 = "output/test3.txt"; private static final String target4 = "output/test4.txt"; public static void main(String[] args) { IOFileCopy.copyFile(source1, target1); NIOFileCopy.copyFile(source2, target2); FilesCopy.copyFile(source3, target3); FileUtilsCopy.copyFile(source4, target4); }}總共執(zhí)行了五次,讀寫的文件大小分別為9KB、23KB、239KB、1.77MB、12.7MB
注意:單位均為毫秒
從執(zhí)行結(jié)果來看:
- 文件很小時 => IO > NIO【內(nèi)存映射】> NIO【管道】 > Files#copy > FileUtils#copyFile
- 在文件較小時 => NIO【內(nèi)存映射】> IO > NIO【管道】 > Files#copy > FileUtils#copyFile
- 在文件較大時 => NIO【內(nèi)存映射】> > NIO【管道】> IO > Files#copy > FileUtils#copyFile
- 修改IO緩沖區(qū)大小對拷貝效率有影響,但是并不是越大性能越好,稍大于拷貝文件大小即可
文件較小時,IO效率高于NIO,NIO底層實(shí)現(xiàn)較為復(fù)雜,NIO的優(yōu)勢不明顯。同時NIO內(nèi)存映射初始化耗時,所以在文件較小時和IO復(fù)制相比沒有優(yōu)勢
如果追求效率可以選擇NIO的內(nèi)存映射去實(shí)現(xiàn)文件拷貝,但是對于大文件使用內(nèi)存映射拷貝要格外關(guān)注系統(tǒng)內(nèi)存的使用率。推薦:大文件拷貝使用內(nèi)存映射,原文是這樣的:
For most operating systems, mapping a file into memory is moreexpensive than reading or writing a few tens of kilobytes of data viathe usual {@link #read read} and {@link #write write} methods. From thestandpoint of performance it is generally only worth mapping relativelylarge files into memory絕大多數(shù)操作系統(tǒng)的內(nèi)存映射開銷大于IO開銷
同時通過測試結(jié)果來看,工具類和JDK提供的文件復(fù)制方法效果并不高,如果不追求效率還是可以使用一下,畢竟能少寫一行代碼就少寫一行代碼,寫代碼沒有摸魚來的快樂
來源:https://juejin.im/post/5e2523686fb9a030051f013e
總結(jié)
以上是生活随笔為你收集整理的system文件_大文件拷贝,试试NIO的内存映射的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 斑能不能彻底去掉_小龙虾的头、虾黄到底能
- 下一篇: vs中列表分页符代码_电脑办公技巧Exc