本章內容包括3個部分:BufferedOutputStream介紹,BufferedOutputStream源碼,以及BufferedOutputStream使用示例。
轉載請注明出處:http://www.cnblogs.com/skywang12345/p/io_13.html
BufferedOutputStream 介紹
BufferedOutputStream 是緩沖輸出流。它繼承於FilterOutputStream。
BufferedOutputStream 的作用是為另一個輸出流提供“緩沖功能”。
BufferedOutputStream 函數列表
BufferedOutputStream(OutputStream out) BufferedOutputStream(OutputStream out, int size) synchronized void close() synchronized void flush() synchronized void write(byte[] buffer, int offset, int length) synchronized void write(int oneByte)
BufferedOutputStream 源碼分析(基於jdk1.7.40)
1 package java.io; 2 3 public class BufferedOutputStream extends FilterOutputStream { 4 // 保存“緩沖輸出流”數據的字節數組 5 protected byte buf[]; 6 7 // 緩沖中數據的大小 8 protected int count; 9 10 // 構造函數:新建字節數組大小為8192的“緩沖輸出流” 11 public BufferedOutputStream(OutputStream out) { 12 this(out, 8192); 13 } 14 15 // 構造函數:新建字節數組大小為size的“緩沖輸出流” 16 public BufferedOutputStream(OutputStream out, int size) { 17 super(out); 18 if (size <= 0) { 19 throw new IllegalArgumentException("Buffer size <= 0"); 20 } 21 buf = new byte[size]; 22 } 23 24 // 將緩沖數據都寫入到輸出流中 25 private void flushBuffer() throws IOException { 26 if (count > 0) { 27 out.write(buf, 0, count); 28 count = 0; 29 } 30 } 31 32 // 將“數據b(轉換成字節類型)”寫入到輸出流中 33 public synchronized void write(int b) throws IOException { 34 // 若緩沖已滿,則先將緩沖數據寫入到輸出流中。 35 if (count >= buf.length) { 36 flushBuffer(); 37 } 38 // 將“數據b”寫入到緩沖中 39 buf[count++] = (byte)b; 40 } 41 42 public synchronized void write(byte b[], int off, int len) throws IOException { 43 // 若“寫入長度”大於“緩沖區大小”,則先將緩沖中的數據寫入到輸出流,然后直接將數組b寫入到輸出流中 44 if (len >= buf.length) { 45 flushBuffer(); 46 out.write(b, off, len); 47 return; 48 } 49 // 若“剩余的緩沖空間 不足以 存儲即將寫入的數據”,則先將緩沖中的數據寫入到輸出流中 50 if (len > buf.length - count) { 51 flushBuffer(); 52 } 53 System.arraycopy(b, off, buf, count, len); 54 count += len; 55 } 56 57 // 將“緩沖數據”寫入到輸出流中 58 public synchronized void flush() throws IOException { 59 flushBuffer(); 60 out.flush(); 61 } 62 }
說明:
BufferedOutputStream的源碼非常簡單,這里就BufferedOutputStream的思想進行簡單說明:BufferedOutputStream通過字節數組來緩沖數據,當緩沖區滿或者用戶調用flush()函數時,它就會將緩沖區的數據寫入到輸出流中。
示例代碼
關於BufferedOutputStream中API的詳細用法,參考示例代碼(BufferedOutputStreamTest.java):
1 import java.io.BufferedOutputStream; 2 import java.io.File; 3 import java.io.OutputStream; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.FileNotFoundException; 7 import java.lang.SecurityException; 8 import java.util.Scanner; 9 10 /** 11 * BufferedOutputStream 測試程序 12 * 13 * @author skywang 14 */ 15 public class BufferedOutputStreamTest { 16 17 private static final int LEN = 5; 18 // 對應英文字母“abcddefghijklmnopqrsttuvwxyz” 19 private static final byte[] ArrayLetters = { 20 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 21 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A 22 }; 23 24 public static void main(String[] args) { 25 testBufferedOutputStream() ; 26 } 27 28 /** 29 * BufferedOutputStream的API測試函數 30 */ 31 private static void testBufferedOutputStream() { 32 33 // 創建“文件輸出流”對應的BufferedOutputStream 34 // 它對應緩沖區的大小是16,即緩沖區的數據>=16時,會自動將緩沖區的內容寫入到輸出流。 35 try { 36 File file = new File("out.txt"); 37 OutputStream out = 38 new BufferedOutputStream( 39 new FileOutputStream(file), 16); 40 41 // 將ArrayLetters數組的前10個字節寫入到輸出流中 42 out.write(ArrayLetters, 0, 10); 43 // 將“換行符\n”寫入到輸出流中 44 out.write('\n'); 45 46 // TODO! 47 //out.flush(); 48 49 readUserInput() ; 50 51 out.close(); 52 } catch (FileNotFoundException e) { 53 e.printStackTrace(); 54 } catch (SecurityException e) { 55 e.printStackTrace(); 56 } catch (IOException e) { 57 e.printStackTrace(); 58 } 59 } 60 61 /** 62 * 讀取用戶輸入 63 */ 64 private static void readUserInput() { 65 System.out.println("please input a text:"); 66 Scanner reader=new Scanner(System.in); 67 // 等待一個輸入 68 String str = reader.next(); 69 System.out.printf("the input is : %s\n", str); 70 } 71 }
運行結果:
生成文件“out.txt”,文件的內容是“abcdefghij”。
分步測試:
分別按照下面3種步驟測試程序,來查看緩沖區大小以及flush()的作用。
第1種:原始程序。
(01) 運行程序。在程序等待用戶輸入時,查看“out.txt”的文本內容;發現:內容為空。
(02) 運行程序。在用戶輸入之后,查看“out.txt”的文本內容;發現:內容為“abcdefghij”。
從中,我們發現(01)和(02)的結果不同;之所以(01)中的out.txt內容為空,是因為out.txt對應的緩沖區大小是16字節,而我們只寫入了11個字節,所以,它不會執行清空緩沖操作(即,將緩沖數據寫入到輸出流中)。
而(02)對應out.txt的內容是“abcdefghij”,是因為執行了out.close(),它會關閉輸出流;在關閉輸出流之前,會將緩沖區的數據寫入到輸出流中。
注意:重新測試時,要先刪除out.txt。
第2種:在readUserInput()前添加如下語句,
out.flush();
這句話的作用,是將“緩沖區的內容”寫入到輸出流中。
(01) 運行程序。在程序等待用戶輸入時,查看“out.txt”的文本內容;發現:內容為“abcdefghij”。
(02) 運行程序。在用戶輸入之后,查看“out.txt”的文本內容;發現:內容為“abcdefghij”。
從中,我們發現(01)和(02)結果一樣,對應out.txt的內容都是“abcdefghij”。這是因為執行了flush()操作,它的作用是將緩沖區的數據寫入到輸出流中。
注意:重新測試時,要先刪除out.txt!
第3種:在第1種的基礎上,將
out.write(ArrayLetters, 0, 10);
修改為
out.write(ArrayLetters, 0, 20);
(01) 運行程序。在程序等待用戶輸入時,查看“out.txt”的文本內容;發現:內容為“abcdefghijklmnopqrst”(不含回車)。
(02) 運行程序。在用戶輸入之后,查看“out.txt”的文本內容;發現:內容為“abcdefghijklmnopqrst”(含回車)。
從中,我們發現(01)運行結果是“abcdefghijklmnopqrst”(不含回車)。這是因為,緩沖區的大小是16,而我們通過out.write(ArrayLetters, 0, 20)寫入了20個字節,超過了緩沖區的大小;這時,會直接將全部的輸入都寫入都輸出流中,而不經過緩沖區。
(02)運行結果是“abcdefghijklmnopqrst”(含回車),這是因為執行out.close()時,將回車符號'\n'寫入了輸出流中。
注意:重新測試時,要先刪除out.txt!
更多內容
03. java io系列02之 ByteArrayInputStream的簡介,源碼分析和示例(包括InputStream)
04. java io系列03之 ByteArrayOutputStream的簡介,源碼分析和示例(包括OutputStream)
05. java io系列04之 管道(PipedOutputStream和PipedInputStream)的簡介,源碼分析和示例
06. java io系列05之 ObjectInputStream 和 ObjectOutputStream
07. java io系列06之 序列化總結(Serializable 和 Externalizable)
08. java io系列07之 FileInputStream和FileOutputStream
10. java io系列09之 FileDescriptor總結
11. java io系列10之 FilterInputStream
12. java io系列11之 FilterOutputStream