最近項(xiàng)目里有個(gè)需求需要實(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中文件讀取過(guò)程可以分為以下幾步:
內(nèi)核從磁盤讀取數(shù)據(jù)到緩沖區(qū)忿薇,這個(gè)過(guò)程由磁盤操作器通過(guò)DMA操作將數(shù)據(jù)從磁盤讀取到內(nèi)核緩沖區(qū)躏哩,該過(guò)程不依賴CPU
用戶進(jìn)程在將數(shù)據(jù)從內(nèi)核緩沖區(qū)拷貝到用戶空間緩沖區(qū)
用戶進(jìn)程從用戶空間緩沖區(qū)讀取數(shù)據(jù)
NIO拷貝
NIO進(jìn)行文件拷貝有兩種實(shí)現(xiàn)方式扫尺,一是通過(guò)管道,而是通過(guò)文件內(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)核空間地址與用戶空間的虛擬地址映射到同一個(gè)物理地址弊攘,DMA 硬件可以填充對(duì)內(nèi)核與用戶空間進(jìn)程同時(shí)可見(jiàn)的緩沖區(qū)了。用戶進(jìn)程直接從內(nèi)存中讀取文件內(nèi)容氓栈,應(yīng)用只需要和內(nèi)存打交道婿着,不需要進(jìn)行緩沖區(qū)來(lái)回拷貝醋界,大大提高了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)存映射文件拷貝可以分為以下幾步
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之前需先引入依賴
-
依賴
<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)); } }
性能比較
既然有這么多種實(shí)現(xiàn)方法,肯定要從中選擇性能最佳的
測(cè)試環(huán)境:
- windows 10
- CPU 6核
- JDK1.8
測(cè)試代碼: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
注意:?jiǎn)挝痪鶠楹撩?/p>
從執(zhí)行結(jié)果來(lái)看:
文件很小時(shí) => IO > NIO【內(nèi)存映射】> NIO【管道】 > Files#copy > FileUtils#copyFile
在文件較小時(shí) => NIO【內(nèi)存映射】> IO > NIO【管道】 > Files#copy > FileUtils#copyFile
在文件較大時(shí) => NIO【內(nèi)存映射】> > NIO【管道】> IO > Files#copy > FileUtils#copyFile
文件較小時(shí)斩启,IO效率高于NIO,NIO底層實(shí)現(xiàn)較為復(fù)雜兔簇,NIO的優(yōu)勢(shì)不明顯。同時(shí)NIO內(nèi)存映射初始化耗時(shí)朴摊,所以在文件較小時(shí)和IO復(fù)制相比沒(méi)有優(yōu)勢(shì)
如果追求效率可以選擇NIO的內(nèi)存映射去實(shí)現(xiàn)文件拷貝此虑,但是對(duì)于大文件使用內(nèi)存映射拷貝要格外關(guān)注系統(tǒng)內(nèi)存的使用率。推薦:大文件拷貝使用內(nèi)存映射介杆,原文是這樣的:
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
絕大多數(shù)操作系統(tǒng)的內(nèi)存映射開(kāi)銷大于IO開(kāi)銷
同時(shí)通過(guò)測(cè)試結(jié)果來(lái)看,工具類和JDK提供的文件復(fù)制方法效果并不高春哨,如果不追求效率還是可以使用一下,畢竟能少寫一行代碼就少寫一行代碼椰拒,寫代碼沒(méi)有摸魚來(lái)的快樂(lè)
年前最后一篇文章燃观,我怕三十晚上的祝福太多便瑟,你會(huì)看不到我的問(wèn)候,在這里提前祝大家新年財(cái)富”鼠“都”鼠“不過(guò)來(lái)