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

main方法測試
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("共耗時"+(end-start)+"毫秒");
}
一、一次讀取一個字節(jié)
//一次讀取一個字節(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)行截圖:

二、一次讀取一個字節(jié)數(shù)組
// 一次讀取一個字節(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ū)一次讀取一個字節(jié)
/ 使用高效緩沖區(qū)一次讀取一個字節(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ū)一次讀取一個字節(jié)數(shù)組
// 使用高效緩沖區(qū)一次讀取一個字節(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)行截圖:

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

