最近項目裏有個需求須要實現文件拷貝,在java中文件拷貝流的讀寫,很容易就想到IO中的InputStream和OutputStream之類的,可是上網查了一下文件拷貝也是有不少種方法的,除了IO,還有NIO、Apache提供的工具類、JDK自帶的文件拷貝方法java
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(); } } }
傳統IO中文件讀取過程能夠分爲如下幾步:windows
內核從磁盤讀取數據到緩衝區,這個過程由磁盤操做器經過DMA操做將數據從磁盤讀取到內核緩衝區,該過程不依賴CPUapp
用戶進程在將數據從內核緩衝區拷貝到用戶空間緩衝區ide
NIO進行文件拷貝有兩種實現方式,一是經過管道,而是經過文件內存內存映射工具
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)); } }
文件內存映射:性能
把內核空間地址與用戶空間的虛擬地址映射到同一個物理地址,DMA 硬件能夠填充對內核與用戶空間進程同時可見的緩衝區了。用戶進程直接從內存中讀取文件內容,應用只須要和內存打交道,不須要進行緩衝區來回拷貝,大大提升了IO拷貝的效率。加載內存映射文件所使用的內存在Java堆區以外測試
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內存映射文件拷貝能夠分爲如下幾步操作系統
NIO的內存映射實際上就是少了一次從內核空間拷貝到用戶空間的過程,將對用戶緩衝區的讀改成從內存讀取code
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以前需先引入依賴orm
依賴
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency>
FileUtils#copyFile封裝類:FileUtilsCopy.java
public 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)); } }
既然有這麼多種實現方法,確定要從中選擇性能最佳的
測試環境:
測試代碼: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); } }
總共執行了五次,讀寫的文件大小分別爲9KB、23KB、239KB、1.77MB、12.7MB
注意:單位均爲毫秒
從執行結果來看:
文件很小時 => IO > NIO【內存映射】> NIO【管道】 > Files#copy > FileUtils#copyFile
在文件較小時 => NIO【內存映射】> IO > NIO【管道】 > Files#copy > FileUtils#copyFile
在文件較大時 => NIO【內存映射】> > NIO【管道】> IO > Files#copy > FileUtils#copyFile
文件較小時,IO效率高於NIO,NIO底層實現較爲複雜,NIO的優點不明顯。同時NIO內存映射初始化耗時,因此在文件較小時和IO複製相比沒有優點
若是追求效率能夠選擇NIO的內存映射去實現文件拷貝,可是對於大文件使用內存映射拷貝要格外關注系統內存的使用率。推薦:大文件拷貝使用內存映射,原文是這樣的:
For most operating systems, mapping a file into memory is more expensive than reading or writing a few tens of kilobytes of data via the usual {@link #read read} and {@link #write write} methods. From the standpoint of performance it is generally only worth mapping relatively large files into memory
絕大多數操做系統的內存映射開銷大於IO開銷
同時經過測試結果來看,工具類和JDK提供的文件複製方法效果並不高,若是不追求效率仍是可使用一下,畢竟能少寫一行代碼就少寫一行代碼,寫代碼沒有摸魚來的快樂