您好,登錄后才能下訂單哦!
最近項目里有個需求需要實現文件拷貝,在java中文件拷貝流的讀寫,很容易就想到IO中的InputStream和OutputStream之類的,但是上網查了一下文件拷貝也是有很多種方法的,除了IO,還有NIO、Apache提供的工具類、JDK自帶的文件拷貝方法
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中文件讀取過程可以分為以下幾步:
內核從磁盤讀取數據到緩沖區,這個過程由磁盤操作器通過DMA操作將數據從磁盤讀取到內核緩沖區,該過程不依賴CPU
用戶進程在將數據從內核緩沖區拷貝到用戶空間緩沖區
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的內存映射實際上就是少了一次從內核空間拷貝到用戶空間的過程,將對用戶緩沖區的讀改為從內存讀取
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之前需先引入依賴
依賴
<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開銷
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。