BufferedWriter繼承自Writer類是字符緩衝輸出流,它經過在內部建立一個字符緩衝區(char數組)爲底層綁定的其餘字符輸出流Writer提供緩衝的功能,在不要求字符數據即時寫入狀況下能夠實現單個字符、數組、字符串的高效寫入。他提供字符、數組等高效寫入的原理:若不在內部提供一個緩衝區,那麼每次寫入操做都要直接操做底層字符輸出流Writer將字符轉爲字節數據而後向目的文件或者其餘目標寫入數據每寫入一次都要先創建鏈接,訪問磁盤,無疑效率是很是低下的,BufferedWriter經過在內部建立一個字符緩衝區,每次寫入的時候先保存到內部緩衝區中,當到達必定數量時再將緩衝區中的字符數據一次性寫入到綁定的底層字符輸出流中,這樣雖然不能實現數據實時寫入到磁盤但提高了效率減小了磁盤訪問次數。java
public class BufferedWriter extends Writer { //底層綁定的字符輸出流 private Writer out; //內部字符數據緩衝區 private char cb[]; //緩衝區大小 private int nChars; //緩衝區下一個字符寫入位置 int nextChar; //默認緩衝區大小 private static int defaultCharBufferSize = 8192; //行分隔符 private String lineSeparator; }
/** * 構造函數,指定綁定的底層字符輸出流,建立一個默認大小的字符輸出緩衝區 */ public BufferedWriter(Writer out) { this(out, defaultCharBufferSize); } /** * 構造函數,指定綁定的底層字符輸出流,建立指定大小的字符輸出緩衝區 */ public BufferedWriter(Writer out, int sz) { super(out); if (sz <= 0) throw new IllegalArgumentException("Buffer size <= 0"); this.out = out; cb = new char[sz]; nChars = sz; nextChar = 0; lineSeparator = java.security.AccessController.doPrivileged( new sun.security.action.GetPropertyAction("line.separator")); }
因爲第一個構造方法內部調用的其實也是第二個構造方法,咱們直接分析下第二個構造方法BufferedWriter(Writer out, int sz),分析源碼該方法內部首先調用了父類構造方法super(out)該方法將BufferedWrite的鎖對象引用lock指向方法指定的底層字符輸出流對象out,接着對方法的緩衝區大小參數sz進行合法性校驗,若小於0拋出異常,接下來就是綁定底層字符輸出流out,建立指定長度sz緩衝區數組,初始化緩衝區下一個字符寫入位置nextChar和緩衝區大小naChars。數組
public void write(int c) throws IOException { synchronized (lock) { //檢查流狀態 ensureOpen(); //緩衝區是否寫滿,若滿則刷新緩衝區 if (nextChar >= nChars) flushBuffer(); //在緩衝區數組寫入字符 cb[nextChar++] = (char) c; } }
這裏方法內部首先調用ensureOpen方法檢測流是否已經關閉,若未關閉判斷緩衝區是否寫滿,若已經寫滿則調用flushBuffer方法刷新緩衝區,咱們進入該方法源碼看下實現:函數
void flushBuffer() throws IOException { synchronized (lock) { ensureOpen(); if (nextChar == 0) return; out.write(cb, 0, nextChar); nextChar = 0; } }
flushBuffer方法判斷了nextChar是否爲0若爲0則說明緩衝區尚未寫入過或者以前已經刷新過流但還沒寫入數據,不然調用out.write方法將當前字符緩衝區數據寫入到底層字符輸出流out中重置緩衝區下一個字符寫入位置nextChar爲0,重複利用緩衝區,覆蓋緩衝區原有的字符數據。源碼分析
分析完flushBuffer方法再回到調用方write方法繼續往下分析,最後將指定字符插入緩衝區數組cb的當前寫入位置。this
總結write(int c)方法的基礎邏輯是首先判斷BufferedWriter的流狀態,若流已經關閉(即底層字符輸出流爲null)則拋出異常,不然判斷緩衝區是否寫滿若寫滿則將緩衝區字符數據寫入到底層字符輸出流後重置緩衝區從新從字符緩衝區數組開頭寫入字符數據,最後將指定字符c寫入緩衝區。spa
/** 檢測BufferedWriter是否關閉 **/ private void ensureOpen() throws IOException { if (out == null) throw new IOException("Stream closed"); } /** * 刷新內部的字符輸出緩衝區,將緩衝區字符數據寫入底層輸出流中,並重置緩衝區下一個寫入字符位置nextChar */ void flushBuffer() throws IOException { synchronized (lock) { ensureOpen(); if (nextChar == 0) return; out.write(cb, 0, nextChar); nextChar = 0; } } /** * 取最小值 */ private int min(int a, int b) { if (a < b) return a; return b; } /** * 寫入字符數組的一部分,從下標off開始的len個字符 */ public void write(char cbuf[], int off, int len) throws IOException { synchronized (lock) { //檢測流的狀態 ensureOpen(); //校驗參數的合法性 if ((off < 0) || (off > cbuf.length) || (len < 0) || ((off + len) > cbuf.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return; } //若字符數組寫入的字符個數len大於緩衝區大小,刷新當前緩衝區將緩衝區字符數據寫入到底層字符輸出流,重置緩 //衝區,以後將要寫入的字符數組數據直接寫入底層字符輸出流 if (len >= nChars) { flushBuffer(); out.write(cbuf, off, len); return; } //若寫入的字符個數小於緩衝區大小則將數據先寫入緩衝區中,在緩衝區寫滿時將數據寫入底層字節輸出流,而後重置 //緩衝區,繼續寫入 int b = off, t = off + len; while (b < t) { int d = min(nChars - nextChar, t - b); System.arraycopy(cbuf, b, cb, nextChar, d); b += d; nextChar += d; if (nextChar >= nChars) flushBuffer(); } } } /** * 寫入一個字符串的一部分,從下標off開始的len個字符寫入到內部字符緩衝區cb中 */ public void write(String s, int off, int len) throws IOException { synchronized (lock) { ensureOpen(); int b = off, t = off + len; while (b < t) { //獲取緩衝區剩餘寫入空間和寫入字符個數len的較小值 int d = min(nChars - nextChar, t - b); //將字符串方法參數指定部分寫入到緩衝區 s.getChars(b, b + d, cb, nextChar); b += d; nextChar += d; //判斷緩衝區是否寫滿,若寫滿刷新緩衝區字符數據到底層字符輸出流,重置緩衝區後繼續寫入直到寫完 if (nextChar >= nChars) flushBuffer(); } } } /** * 寫入一個行分隔符 */ public void newLine() throws IOException { write(lineSeparator); } /** * 刷新流,刷新緩衝區數據將其寫入底層字符輸出流中 */ public void flush() throws IOException { synchronized (lock) { flushBuffer(); out.flush(); } } /** * 關閉流,主要是將刷新緩衝區將未寫入底層字符輸出流的數據寫入底層字符輸出流,而後將底層字符輸出流和緩衝區設置爲 * null釋放對象資源 */ public void close() throws IOException { synchronized (lock) { if (out == null) { return; } try (Writer w = out) { flushBuffer(); } finally { out = null; cb = null; } } }