天天看點

javaSE 緩沖流, 位元組緩沖流BufferedOutputStream (提高位元組寫入速度)

Demo.java:

package cn.xxx.demo;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 *  位元組輸出流的緩沖流
 *   java.io.BufferedOuputStream 作用: 提高原有輸出流的寫入效率
 *   BufferedOuputStream 繼承 OutputStream
 *   方法,寫入 write 位元組,位元組數組
 *   
 *   構造方法:
 *     BufferedOuputStream(OuputStream out)
 *     可以傳遞任意的位元組輸出流, 傳遞的是哪個位元組流,就對哪個位元組流提高效率
 *     
 *     FileOutputStream
 */
public class Demo {
	public static void main(String[] args)throws IOException {
		//建立位元組輸出流,綁定檔案
		//FileOutputStream fos = new FileOutputStream("c:\\buffer.txt");
		//建立位元組輸出流緩沖流的對象,構造方法中,傳遞位元組輸出流
		BufferedOutputStream bos = new
				BufferedOutputStream(new FileOutputStream("c:\\buffer.txt"));
		
		bos.write(55);  // 寫一個位元組  int=>byte
		
		byte[] bytes = "HelloWorld".getBytes();
		
		bos.write(bytes);  // 寫多個位元組, 位元組數組
		
		bos.write(bytes, 3, 2);  // 寫位元組數組中的一部分。  索引  長度
		
		bos.close();
	}
}