溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java中IO字節(jié)流之實(shí)現(xiàn)復(fù)制文件并測(cè)試性能的示例分析

發(fā)布時(shí)間:2021-08-26 11:44:41 來(lái)源:億速云 閱讀:91 作者:小新 欄目:編程語(yǔ)言

這篇文章將為大家詳細(xì)講解有關(guān)Java中IO字節(jié)流之實(shí)現(xiàn)復(fù)制文件并測(cè)試性能的示例分析,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

以復(fù)制文件的形式來(lái)演示IO字節(jié)流的基本操作,復(fù)制一個(gè)mp3文件,文件信息如下圖:

Java中IO字節(jié)流之實(shí)現(xiàn)復(fù)制文件并測(cè)試性能的示例分析

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)行截圖:

Java中IO字節(jié)流之實(shí)現(xiàn)復(fù)制文件并測(cè)試性能的示例分析

二、一次讀取一個(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)行截圖:

Java中IO字節(jié)流之實(shí)現(xiàn)復(fù)制文件并測(cè)試性能的示例分析

三、使用高效緩沖區(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)行截圖:

Java中IO字節(jié)流之實(shí)現(xiàn)復(fù)制文件并測(cè)試性能的示例分析

四、使用高效緩沖區(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)行截圖:

Java中IO字節(jié)流之實(shí)現(xiàn)復(fù)制文件并測(cè)試性能的示例分析

注:每臺(tái)測(cè)試的速度結(jié)果不一樣

關(guān)于“Java中IO字節(jié)流之實(shí)現(xiàn)復(fù)制文件并測(cè)試性能的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI