詳解Java中IO字節(jié)流基本操作(復(fù)制文件)并測(cè)試性能
此次案例將以復(fù)制文件的形式來(lái)演示IO字節(jié)流的基本操作,復(fù)制一個(gè)mp3文件,文件信息如下圖:

main方法測(cè)試
public static void main(String[] args) throws Exception {
//源文件
String srcFile = "src/a.mp3";
//目的文件
String destFile = "src/b.mp3";
long start = System.currentTimeMillis();
...
復(fù)制文件方法
...
long end = System.currentTimeMillis();
System.out.println("共耗時(shí)"+(end-start)+"毫秒");
}
一、一次讀取一個(gè)字節(jié)
//一次讀取一個(gè)字節(jié)
public static void copy1(String srcFile,String destFile) throws Exception {
//封裝文件
InputStream in = new FileInputStream(srcFile);
OutputStream out = new FileOutputStream(destFile);
//復(fù)制文件
int b = 0;
while ((b = in.read()) != -1) {
out.write(b);
}
//釋放資源
in.close();
out.close();
}
運(yùn)行截圖:

二、一次讀取一個(gè)字節(jié)數(shù)組
// 一次讀取一個(gè)字節(jié)數(shù)組
public static void copy2(String srcFile, String destFile) throws Exception {
// 封裝文件
InputStream in = new FileInputStream(srcFile);
OutputStream out = new FileOutputStream(destFile);
// 復(fù)制文件
byte[] buff = new byte[1024];
int len = 0;
while ((len = in.read(buff)) != -1) {
out.write(buff, 0, len);
}
// 釋放資源
in.close();
out.close();
}
運(yùn)行截圖:

三、使用高效緩沖區(qū)一次讀取一個(gè)字節(jié)
/ 使用高效緩沖區(qū)一次讀取一個(gè)字節(jié)
public static void copy3(String srcFile, String destFile) throws Exception {
// 封裝文件
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
// 復(fù)制文件
int b = 0;
while ((b = bis.read()) != -1) {
bos.write(b);
}
// 釋放資源
bis.close();
bos.close();
}
運(yùn)行截圖:

四、使用高效緩沖區(qū)一次讀取一個(gè)字節(jié)數(shù)組
// 使用高效緩沖區(qū)一次讀取一個(gè)字節(jié)數(shù)組
public static void copy4(String srcFile, String destFile) throws Exception {
// 封裝文件
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
// 復(fù)制文件
byte[] buf = new byte[1024];
int len = 0;
while ((len = bis.read(buf)) != -1) {
bos.write(buf, 0, len);
}
// 釋放資源
bis.close();
bos.close();
}
運(yùn)行截圖:

注:每臺(tái)測(cè)試的速度結(jié)果不一樣
以上所述是小編給大家介紹的Java中IO字節(jié)流基本操作(復(fù)制文件)并測(cè)試性能,詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
SpringData JPA審計(jì)功能(@CreatedDate與@LastModifiedDate)實(shí)現(xiàn)
Spring Data JPA的審計(jì)功能提供了一種強(qiáng)大而靈活的機(jī)制,用于自動(dòng)跟蹤實(shí)體的創(chuàng)建和修改信息,通過(guò)使用@CreatedDate和@LastModifiedDate注解,開發(fā)者可以輕松地實(shí)現(xiàn)時(shí)間審計(jì),感興趣的可以了解一下2025-04-04
解決IDEA?2022?Translation?翻譯文檔失敗:?未知錯(cuò)誤的問(wèn)題
這篇文章主要介紹了IDEA?2022?Translation?翻譯文檔失敗:?未知錯(cuò)誤,本文較詳細(xì)的給大家介紹了IDEA?2022?Translation未知錯(cuò)誤翻譯文檔失敗的解決方法,需要的朋友可以參考下2022-04-04
深入了解Java SpringBoot自動(dòng)裝配原理
在使用springboot時(shí),很多配置我們都沒(méi)有做,都是springboot在幫我們完成,這很大一部分歸功于springboot自動(dòng)裝配。本文將詳細(xì)為大家講解SpringBoot的自動(dòng)裝配原理,需要的可以參考一下2022-03-03
解決Elasticsearch因jdk版本問(wèn)題啟動(dòng)失敗的問(wèn)題
這篇文章主要介紹了解決Elasticsearch因jdk版本問(wèn)題啟動(dòng)失敗的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
深入理解Java @Entity注解及其與數(shù)據(jù)庫(kù)表的關(guān)聯(lián)
這篇文章主要介紹了深入理解Java @Entity注解及其與數(shù)據(jù)庫(kù)表的關(guān)聯(lián),@Entity注解在JPA中占據(jù)核心地位,它建立起Java實(shí)體類和數(shù)據(jù)庫(kù)表之間的映射關(guān)系,需要的朋友可以參考下2025-05-05
MyBatis-Plus如何使用枚舉自動(dòng)關(guān)聯(lián)注入詳解
這篇文章主要給大家介紹了關(guān)于MyBatis-Plus如何使用枚舉自動(dòng)關(guān)聯(lián)注入的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用MyBatis-Plus具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03

