1. 程式人生 > 實用技巧 >Java—緩衝流

Java—緩衝流

一、緩衝流

高效流,一般指預設聲明瞭一個byte空間(容器)作為緩衝區,比我們一般申請的空間64k大;所以較為高效。

若我們要使FileInputStream流高效,把空間申請得最大即可實現。


二、位元組緩衝流

2.1 位元組緩衝輸出流BufferedOutputStream

public class BufferedStreamDemo {
	public static void main(String[] args) throws IOException {
		// 寫操作
		write();
	}

	public static void write() throws IOException {
		// 建立流
		OutputStream os = new FileOutputStream("e:/text.txt");
		BufferedOutputStream bos = new BufferedOutputStream(os);
		
		// 寫資料
		bos.write("hello".getBytes());
		
		// 關閉流
		bos.close();
		os.close();
	}
}

2.2 位元組緩衝輸入流BufferedInputStream

public class BufferedStreamDemo {
	public static void main(String[] args) throws IOException {
		// 讀操作
		read();
	}

	public static void read() throws IOException {
		// 建立流
		InputStream is = new FileInputStream("e:/text.txt");
		BufferedInputStream bis = new BufferedInputStream(is);
		
		// 讀取資料
		int ch = -1;
		while( (ch = bis.read()) != -1 ){
			System.out.println((char) ch);
		}
		
		// 關閉流
		bis.close();
		is.close();
	}
}

2.3 不同流複製檔案的寫法

public class CopyAVI {
	/*
	 * 採用四種方式複製
	 * 方式一:採用基本的流,一次一個位元組的方式複製	no4
	 * 方式二:採用基本的流,一次多個位元組的方式複製	no2
	 * 方式三:採用高效的流,一次一個位元組的方式複製	no3
	 * 方式四:採用高效的流,一次多個位元組的方式複製	no1
	 */
	public static void main(String[] args) throws IOException {
		long start = System.currentTimeMillis();
		
		// 方式一:
		//method1("E:/test.avi");
		
		// 方式二:
		//method2("E:/test.avi");
		
		// 方式三:
		//method3("E:/test.avi");
		
		// 方式四:
		method4("E:/test.avi");
		
		long end = System.currentTimeMillis();
		
		// 輸入計算用時
		System.out.println(end - start);
	}

	/*
	 * 方式四:採用高效的流,一次多個位元組的方式複製
	 */
	public static void method4(String file) throws IOException {
		// 建立緩衝輸入流
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
		
		// 建立緩衝輸出流
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("e:/copy4.avi"));
		
		byte[] b = new byte[1024 * 8 * 8];
		int len;
		while( (len = bis.read(b)) != -1 ){
			bos.write(b, 0, len);
		}
		
		// 關閉流
		bis.close();
		bos.close();
	}

	/*
	 * 方式三:採用高效的流,一次一個位元組的方式複製
	 */
	private static void method3(String file) throws IOException {
		// 建立緩衝輸入流
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
		
		// 建立緩衝輸出流
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("e:/copy2.avi"));
		
		int ch = -1;
		while( (ch = bis.read()) != -1 ){
			bos.write(ch);
		}
		
		// 關閉流
		bis.close();
		bos.close();
	}

	/*
	 * 方式二:採用基本的流,一次多個位元組的方式複製
	 */
	private static void method2(String file) throws IOException {
		InputStream is = new FileInputStream(file);
		OutputStream os = new FileOutputStream("e:/copy2.avi");
		
		byte[] b = new byte[1024 * 8 * 8];
		int len;
		while( (len = is.read(b)) != -1 ){
			os.write(b, 0, len);
		}
		
		is.close();
		os.close();
	}

	/*
	 * 方式一:採用基本的流,一次一個位元組的方式複製
	 */
	public static void method1(String file) throws IOException {
		InputStream is = new FileInputStream(file);
		OutputStream os = new FileOutputStream("e:/copy1.avi");
		
		int ch = -1;
		while( (ch = is.read()) != -1 ){
			os.write(ch);
		}
		
		is.close();
		os.close();
	}
}

三、字元緩衝流

字元輸出流:BufferedWriter

字元輸入流:BufferedReader


3.1 BufferedWriter類

public class BufferedWriterDemo {
	public static void main(String[] args) throws IOException {
		// 建立流
		Writer writer = new FileWriter("e:/text.txt");
		BufferedWriter bw = new BufferedWriter(writer);
		
		// 寫資料
		for(int i = 0; i < 5; i++){
			bw.write("hello");
			bw.newLine();   //寫入一個分隔符
		}
		
		// 關閉流
		bw.close();
		writer.close();
	}
}

3.2 BufferedReader類

public class BufferedReaderDemo {
	public static void main(String[] args) throws IOException {
		// 建立流
		BufferedReader br = new BufferedReader(new FileReader("e:/text.txt"));
		
		// 讀取資料
		//一次一個字元
		//一次一個字元陣列
		//一次讀取文字中一行的字串內容
		String line;
		while( (line = br.readLine()) != null ){
			System.out.println(line);
		}
		
		// 關閉流
		br.close();
	}
}

3.3 使用緩衝流複製檔案

public class CopyFileByBuffered {
	public static void main(String[] args) throws IOException {
		// 建立流
		BufferedReader br = new BufferedReader(new FileReader("e:/text.txt"));
		BufferedWriter bw = new BufferedWriter(new FileWriter("e:/text-副本.txt"));
		
		// 讀取
		// 複製 —— 寫入
		String line;
		while( (line = br.readLine()) != null ){
			bw.write(line);
			// 換行
			bw.newLine();
		}
		
		// 關閉流
		br.close();
		bw.close();
	}
}