歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> NIO拷貝文件真的比IO拷貝文件效率高?

NIO拷貝文件真的比IO拷貝文件效率高?

日期:2017/3/1 9:43:14   编辑:Linux編程

今天發現項目中有個FileUtils.copy的工具方法, 打開後發現是基於io的, 我給改成了基於NIO的, 突然疑慮NIO拷貝文件真的如其他人說的那樣比IO效率高很多麼?

以下是我的工具方法:

/**
*
* 通過NIO進行文件拷貝
* @param fromFile 被拷貝的文件
* @param toFile 拷貝後的文件
* @throws IOException
*/
public static void copy(String fromFile, String toFile) throws IOException {
FileInputStream inputStream = new FileInputStream(fromFile);
FileChannel fromChannel = inputStream.getChannel();

FileOutputStream outputStream = new FileOutputStream(toFile);
FileChannel toChannel = outputStream.getChannel();

toChannel.transferFrom(fromChannel, 0, fromChannel.size());
// fromChannel.transferTo(0, fromChannel.size(), toChannel);

toChannel.force(true);
inputStream.close();
fromChannel.close();
outputStream.close();
toChannel.close();
}

/**
*
* 使用IO拷貝文件
* @param fromFile 被拷貝的文件
* @param toFile 拷貝後的文件
* @throws IOException
*/
public static void copyByIO(String fromFile, String toFile) throws IOException {
File inputFile = new File(fromFile);
File outputFile = new File(toFile);
FileInputStream inputStream = new FileInputStream(inputFile);
FileOutputStream outputStream = new FileOutputStream(outputFile);
byte[] bytes = new byte[1024];
int c;
while ((c = inputStream.read(bytes)) != -1)
outputStream.write(bytes, 0, c);
inputStream.close();
outputStream.close();
}

以下是測試方法:

@Test
//8.72M文本文件-->拷貝100次: 8781 1000次:101564
//4.65G壓縮包同文件夾拷貝1次:136160 跨盤拷貝1次:147363
public void testCopyNIO() {
String from = "d:/test/test.zip";
Long start = System.currentTimeMillis();
try {
for(int i=0;i<1;i++) {
String to = "e:/test/test"+i+".zip";
FileUtils.copy(from, to);
}

} catch (IOException e) {
e.printStackTrace();
}
Long end = System.currentTimeMillis();
System.out.println(end-start);
}

@Test
//8.72M文本文件-->拷貝100次: 7719 1000次:109051
//4.65G壓縮包同文件夾拷貝1次:103261 跨盤拷貝1次:76799
public void testCopyIO() {
String from = "d:/test/test.zip";
Long start = System.currentTimeMillis();
try {
for(int i=0;i<1;i++) {
String to = "e:/test/test"+i+".zip";
FileUtils.copyByIO(from, to);
}

} catch (Exception e) {
e.printStackTrace();
}
Long end = System.currentTimeMillis();
System.out.println(end-start);
}

第一次我找了個sysbase通過bcp命令導出的數據文件, 大小為8718KB, 使用以上兩個測試方法, 分別拷貝了100次, 發現NIO執行時間為8781毫秒,IO執行時間為7719毫秒, NIO輸了。

第二次我將拷貝次數改成了1000, 發現NIO執行時間為101564毫秒,IO執行時間為109051毫秒, NIO贏了, 但也僅僅贏了不到8秒。

第三次我將數據文件復制打包再復制再打包最後做出一個4,650,673KB大小的壓縮包, 由於磁盤空間問題, 這次我只執行了1次, 發現NIO執行時間為136160毫秒,IO執行時間為103261毫秒, NIO輸了33秒。

我又懷疑同一個文件夾下面拷貝可能沒有突出NIO的優勢,於是我又做了第四次測試, 換了個磁盤, 結果發現NIO執行時間為147363毫秒,IO執行時間為76799毫秒, NIO輸了更慘, 耗費時間足足是IO的兩倍。

可見NIO雖然在很多方面比IO強,但是這也並不是絕對的。

以上只是針對NIO和普通IO的簡單測試, 並沒有深究文件拷貝, 有興趣的兄弟可以去研究下通過BufferedInputStream和RandomAccessFile來進行文件拷貝。其中RandomAccessFile效率應該和FileInputStream差不多, BufferedInputStream肯定比其他的要高效很多。

Copyright © Linux教程網 All Rights Reserved