1. 程式人生 > 程式設計 >java 實現位元組流和位元組緩衝流讀寫檔案時間對比

java 實現位元組流和位元組緩衝流讀寫檔案時間對比

我就廢話不多說了,大家還是直接看程式碼吧~

package cn.itcast.copy; 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
 
/*
 * 檔案複製方式,位元組流,一共4個方式
 * 1. 位元組流讀寫單個位元組     125250 毫秒
 * 2. 位元組流讀寫位元組陣列     193 毫秒 OK
 * 3. 位元組流緩衝區流讀寫單個位元組  1210 毫秒
 * 4. 位元組流緩衝區流讀寫位元組陣列  73  毫秒 OK
 */
public class Copy {
	public static void main(String[] args)throws IOException {
		long s = System.currentTimeMillis();
		copy_4(new File("c:\\q.exe"),new File("d:\\q.exe"));
		long e = System.currentTimeMillis();
		System.out.println(e-s);
	}
	/*
	 * 方法,實現檔案複製
	 * 4. 位元組流緩衝區流讀寫位元組陣列
	 */
	public static void copy_4(File src,File desc)throws IOException{
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));
		int len = 0 ;
		byte[] bytes = new byte[1024];
		while((len = bis.read(bytes))!=-1){
			bos.write(bytes,len);
		}
		bos.close();
		bis.close();
	}
	/*
	 * 方法,實現檔案複製
	 * 3. 位元組流緩衝區流讀寫單個位元組
	 */
	public static void copy_3(File src,File desc)throws IOException{
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));
		int len = 0 ;
		while((len = bis.read())!=-1){
			bos.write(len);
		}
		bos.close();
		bis.close();
	}
	
	/*
	 * 方法,實現檔案複製
	 * 2. 位元組流讀寫位元組陣列
	 */
	public static void copy_2(File src,File desc)throws IOException{
		FileInputStream fis = new FileInputStream(src);
		FileOutputStream fos = new FileOutputStream(desc);
		int len = 0 ;
		byte[] bytes = new byte[1024];
		while((len = fis.read(bytes))!=-1){
			fos.write(bytes,len);
		}
		fos.close();
		fis.close();
	}
	
	/*
	 * 方法,實現檔案複製
	 * 1. 位元組流讀寫單個位元組
	 */
	public static void copy_1(File src,File desc)throws IOException{
		FileInputStream fis = new FileInputStream(src);
		FileOutputStream fos = new FileOutputStream(desc);
		int len = 0 ;
		while((len = fis.read())!=-1){
			fos.write(len);
		}
		fos.close();
		fis.close();
	}
}

補充:輸入流輸出流快速讀寫方式

這是以前整理的,今天看到了,就放到部落格中!

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
 
public class Demo {
 public static void main(String[] args) throws IOException {
 
 // 獲取開始時間
 long start = System.currentTimeMillis();
 
 // 1. 建立一個檔案位元組輸入流物件,關聯原始檔
 InputStream in = new FileInputStream("C:\\Users\\Jack\\temp\\柳巖.jpg");
 
 // 2. 建立一個檔案位元組輸出流物件,關聯目標檔案
 File file = new File("C:\\Users\\Jack\\myDoc\\ly.jpg");
 if (!file.exists()) {
 // 如果檔案不存在,就需要建立
 File parentFile = file.getParentFile();
 parentFile.mkdirs();
 }
 OutputStream out = new FileOutputStream(file);
 
 // 3. 讀取與寫入
 byte[] buf = new byte[1024]; //分配1024個位元組大小的記憶體給buf
 int len = -1;
 while ((len = in.read(buf)) != -1) {
 out.write(buf,len);
 }
 
 // 4. 關閉資源
 out.close();
 in.close();
 
 // 獲取結束時間
 long end = System.currentTimeMillis();
 
 System.out.println("毫秒: " + (end - start));
 }
}

注:

File file = new File("C:\Users\Jack\myDoc\ly.jpg"); 

new File(檔案路徑名稱),方法裡面如果只寫了檔名。格式,這是絕對路徑,位置在當前的工作空間裡面。

以上為個人經驗,希望能給大家一個參考,也希望大家多多支援我們。如有錯誤或未考慮完全的地方,望不吝賜教。