事實證明BufferedInputStream比FileInputStream快,而不是老師說的那個結論批量比緩沖塊??!如果批量緩沖與批量相比呢?代碼如下
/**
?*?批量讀寫
?*
?*?@param?srcFile
?*?@param?destFile
?*/
public?static?void?copyFile(File?srcFile,?File?destFile)?{
????FileInputStream?inputStream?=?null;
????FileOutputStream?outputStream?=?null;
????if?(!srcFile.exists())?{
????????throw?new?IllegalArgumentException("源目標文件不存在?。。?);
????}
????if?(!srcFile.isFile())?{
????????throw?new?IllegalArgumentException("源目標不是文件類型?。?!");
????}
????try?{
????????inputStream?=?new?FileInputStream(srcFile);
????????outputStream?=?new?FileOutputStream(destFile);
????????byte[]?bytes?=?new?byte[8?*?1024];
????????int?length?=?0;
????????while?((length?=?inputStream.read(bytes,?0,?bytes.length))?!=?-1)?{
????????????outputStream.write(bytes,?0,?length);
????????????outputStream.flush();
????????}
????}?catch?(IOException?e)?{
????????e.printStackTrace();
????}?finally?{
????????try?{
????????????outputStream.close();
????????????inputStream.close();
????????}?catch?(IOException?e)?{
????????????e.printStackTrace();
????????}
????}
}
/**
?*?緩沖批量讀寫
?*
?*?@param?srcFile
?*?@param?destFile
?*/
public?static?void?copyFileByBuffer(File?srcFile,?File?destFile)?{
????FileInputStream?inputStream?=?null;
????BufferedInputStream?bufferedInputStream?=?null;
????FileOutputStream?outputStream?=?null;
????BufferedOutputStream?bufferedOutputStream?=?null;
????if?(!srcFile.exists())?{
????????throw?new?IllegalArgumentException("源目標文件不存在?。?!");
????}
????if?(!srcFile.isFile())?{
????????throw?new?IllegalArgumentException("源目標不是文件類型!??!");
????}
????try?{
????????inputStream?=?new?FileInputStream(srcFile);
????????bufferedInputStream?=?new?BufferedInputStream(inputStream);
????????outputStream?=?new?FileOutputStream(destFile);
????????bufferedOutputStream?=?new?BufferedOutputStream(outputStream);
????????byte[]?bytes?=?new?byte[8?*?1024];
????????int?length?=?0;
????????while?((length?=?bufferedInputStream.read(bytes,?0,?bytes.length))?!=?-1)?{
????????????bufferedOutputStream.write(bytes,?0,?length);
????????????outputStream.flush();
????????}
????}?catch?(IOException?e)?{
????????e.printStackTrace();
????}?finally?{
????????try?{
????????????bufferedOutputStream.close();
????????????bufferedInputStream.close();
????????}?catch?(IOException?e)?{
????????????e.printStackTrace();
????????}
????}
}測試代碼:
public?class?IOUtilTest?{
????public?static?void?main(String[]?args)?throws?IOException?{
????????File?srcFile1?=?new?File("F:\\學習資料\\1-1和2.mp4");
????????File?destFile1?=?new?File("F:\\1.mp4");
????????long?beg1?=?System.currentTimeMillis();
????????IOUtil.copyFile(srcFile1,destFile1);
????????long?end1?=?System.currentTimeMillis();
????????System.out.println("批量讀取時間毫秒:"+(end1?-?beg1));//768
????????File?destFile2?=?new?File("F:\\2.mp4");
????????long?beg2?=?System.currentTimeMillis();
????????IOUtil.copyFileByBuffer(srcFile1,destFile2);
????????long?end2?=?System.currentTimeMillis();
????????System.out.println("批量緩沖讀取時間毫秒:"+(end2?-?beg2));//130
????}
}源目標文件大?。?0.4M
2019-04-02
這里的對比條件并不成立,并不能說誰比誰快
而是理解緩沖是為了減少I/O,可以理解為減少寫次數,比如讀出一個字節就去寫入,和全部讀出放入緩沖區存儲最后一次寫入,當時是后者更好
上面的同學舉的例子:
在批量讀取、和緩沖批量讀中均是全部讀出一次寫入,其實并沒有用到緩沖的優勢
2018-12-17
理論上是不通的。
按道理應該是批量比緩沖更快??梢钥匆幌略创a,知道緩沖底層調的是批量。如果數據不對,試一下多試幾次。有可能是JVM垃圾收集影響了。還有要拷貝不同的文件。讀過的文件操作系統有緩存了??揭粋€1-2GB的文件試試。
?public?static?void?main(String[]?args)?throws?IOException?{????????File?srcFile1?=?new?File("logs/input.iso");????????File?destFile1?=?new?File("logs/output.iso");????????long?beg1?=?System.currentTimeMillis();????????copyFileByBatch(srcFile1,destFile1);????????long?end1?=?System.currentTimeMillis();????????System.out.println("批量讀取時間毫秒:"+(end1?-?beg1));//1261????????File?srcFile2?=?new?File("logs/input2.iso");????????File?destFile2?=?new?File("logs/output2.iso");????????long?beg2?=?System.currentTimeMillis();????????copyFileByBuffer(srcFile2,destFile2);????????long?end2?=?System.currentTimeMillis();????????System.out.println("批量緩沖讀取時間毫秒:"+(end2?-?beg2));//1899????}2018-10-16
我看的時候也在想這個問題。謝謝給出答案!
2018-08-12
受教了,我剛才還鬧不清楚批量比緩沖快還用緩存干嘛,現在想明白啦。鬧混了對比的對象應該是單字節跟緩沖對比,批量跟批量緩沖對比。
2017-12-21
因為用了緩存~