Java輸入輸出流復(fù)制文件所用時(shí)間對(duì)比
廢話不多說,關(guān)鍵代碼如下所述:
package com.edu.xynu; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class IOUnitCopy { //按字節(jié) public static void copyByByte(File srcFile,File destFile) throws IOException{ FileInputStream fis=new FileInputStream(srcFile); FileOutputStream fos=new FileOutputStream(destFile); int i; while((i=fis.read())!=-1){ fos.write(i); } fis.close(); fos.close(); } //按字節(jié)數(shù)組 public static void copyByByteArray(File srcFile,File destFile) throws IOException{ FileInputStream fis=new FileInputStream(srcFile); FileOutputStream fos=new FileOutputStream(destFile); byte []buf=new byte[10*1024]; int i; while((i=fis.read(buf, 0, buf.length))!=-1){ fos.write(buf, 0, i); } fis.close(); fos.close(); } //字節(jié)緩沖流 public static void copyByBuff(File srcFile,File destFile) throws IOException{ BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcFile)); BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destFile)); int i; while((i=bis.read())!=-1){ bos.write(i); } bos.flush(); bis.close(); bos.close(); } //字節(jié)數(shù)組批量讀取 緩沖輸出流寫入 public static void copyByBuffArray(File srcFile,File destFile) throws IOException{ FileInputStream bis=new FileInputStream(srcFile); BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destFile)); byte [] buf=new byte[10*1024]; int len; while((len=bis.read(buf,0,buf.length))!=-1){ bos.write(buf,0,len); } bos.flush(); bis.close(); bos.close(); } } package com.edu.xynu; import java.io.File; import java.io.IOException; public class IOUnitsCopyTest { public static void main(String[] args) { // TODO Auto-generated method stub try { long start=System.currentTimeMillis(); // IOUnitCopy.copyByByte(new File("c:\\1.mp3"), new File( // "c:\\2.mp3"));//90713ms // IOUnitCopy.copyByByteArray(new File("c:\\1.mp3"), new File( // "c:\\3.mp3"));//41ms // IOUnitCopy.copyByBuff(new File("c:\\1.mp3"), new File( // "c:\\4.mp3"));//556ms // IOUnitCopy.copyByByteArray(new File("c:\\1.mp3"), new File( // "c:\\5.mp3"));//30ms long end=System.currentTimeMillis(); System.out.println(end-start); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
測(cè)試文件是
相關(guān)文章
Java驗(yàn)證時(shí)間格式是否正確方法類項(xiàng)目實(shí)戰(zhàn)
在很多場(chǎng)景中我們需要驗(yàn)證時(shí)間日期的是否屬于正確的格式,驗(yàn)證時(shí)間是否符合常規(guī)的,本文就來介紹一下幾種方式,感興趣的可以了解一下2022-04-04Http Cookie機(jī)制及Cookie的實(shí)現(xiàn)原理
Cookie是進(jìn)行網(wǎng)站用戶身份,實(shí)現(xiàn)服務(wù)端Session會(huì)話持久化的一種非常好方式。Cookie最早由Netscape公司開發(fā),現(xiàn)在由 IETF 的RFC 6265標(biāo)準(zhǔn)備對(duì)其規(guī)范,已被所有主流瀏覽器所支持2021-06-06帶你了解Java數(shù)據(jù)結(jié)構(gòu)和算法之2-3-4樹
這篇文章主要為大家介紹了Java數(shù)據(jù)結(jié)構(gòu)和算法之2-3-4樹,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-01-01Java?web實(shí)現(xiàn)購(gòu)物車案例
這篇文章主要為大家詳細(xì)介紹了Java?web實(shí)現(xiàn)購(gòu)物車案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08idea 無法創(chuàng)建Scala class 選項(xiàng)的原因分析及解決辦法匯總
這篇文章主要介紹了idea 無法創(chuàng)建Scala class 選項(xiàng)的解決辦法匯總,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09Java使用@EnableEurekaServer實(shí)現(xiàn)自動(dòng)裝配詳解
這篇文章主要介紹了Java使用@EnableEurekaServer實(shí)現(xiàn)自動(dòng)裝配過程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-10-10自己動(dòng)手用Springboot實(shí)現(xiàn)仿百度網(wǎng)盤的實(shí)踐
本項(xiàng)目基于Springboot開發(fā)實(shí)現(xiàn),前端采用BootStrap開發(fā)實(shí)現(xiàn),模仿百度網(wǎng)盤實(shí)現(xiàn)相關(guān)功能,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12