事實證明BufferedInputStream比FileInputStream快,而不是老師說的那個結(jié)論批量比緩沖塊?。∪绻烤彌_與批量相比呢?代碼如下
/** ?*?批量讀寫 ?* ?*?@param?srcFile ?*?@param?destFile ?*/ public?static?void?copyFile(File?srcFile,?File?destFile)?{ ????FileInputStream?inputStream?=?null; ????FileOutputStream?outputStream?=?null; ????if?(!srcFile.exists())?{ ????????throw?new?IllegalArgumentException("源目標(biāo)文件不存在!?。?); ????} ????if?(!srcFile.isFile())?{ ????????throw?new?IllegalArgumentException("源目標(biāo)不是文件類型?。?!"); ????} ????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("源目標(biāo)文件不存在!??!"); ????} ????if?(!srcFile.isFile())?{ ????????throw?new?IllegalArgumentException("源目標(biāo)不是文件類型?。?!"); ????} ????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:\\學(xué)習(xí)資料\\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 ????} }
源目標(biāo)文件大?。?0.4M
2019-04-02
這里的對比條件并不成立,并不能說誰比誰快
而是理解緩沖是為了減少I/O,可以理解為減少寫次數(shù),比如讀出一個字節(jié)就去寫入,和全部讀出放入緩沖區(qū)存儲最后一次寫入,當(dāng)時是后者更好
上面的同學(xué)舉的例子:
在批量讀取、和緩沖批量讀中均是全部讀出一次寫入,其實并沒有用到緩沖的優(yōu)勢
2018-12-17
理論上是不通的。
按道理應(yīng)該是批量比緩沖更快??梢钥匆幌略创a,知道緩沖底層調(diào)的是批量。如果數(shù)據(jù)不對,試一下多試幾次。有可能是JVM垃圾收集影響了。還有要拷貝不同的文件。讀過的文件操作系統(tǒng)有緩存了。拷一個1-2GB的文件試試。
2018-10-16
我看的時候也在想這個問題。謝謝給出答案!
2018-08-12
受教了,我剛才還鬧不清楚批量比緩沖快還用緩存干嘛,現(xiàn)在想明白啦。鬧混了對比的對象應(yīng)該是單字節(jié)跟緩沖對比,批量跟批量緩沖對比。
2017-12-21
因為用了緩存~