java.io.BufferedWriter API 以及源碼解讀

來源:互聯網
上載者:User

標籤:文本   throw   cee   應該   div   closed   efault   大小   illegal   

下面是java se 7 API 對於java.io.BufferedWriter 繼承關係的描述。

BufferedWriter可以將文本寫入字元流。它會將字元緩衝,目的是提高寫入字元的效率。

buffer的大小必須明確,否則將會使用預設的大小。預設的大小對於大多數情況是足夠大的。

BufferedWriter提供了一個newLine()的方法,目的是用來換行。畢竟不是所有的平台都使用‘\n‘的換行方式。

一個Writer對象會將輸出立即寫入當前的字元流或者位元組流。

通常來說,如果這個寫入不是非常緊急,建議使用BufferedWriter對象包裹一個費時的Writer對象(比如,FileWriters,OutputStreamWriters)。

具體例子如下:

PrintWriter out = new PrintWriter(new BufferWriter(new FileWriter("foo.out")));

上面這個例子,會將PrintWriter對象的輸出緩衝到檔案。如果沒有緩衝,每次調用print()方法都會講要輸出的字元立即轉換為位元組,這樣的話寫入的效率非常不高。

————————————————————————————————————————————————————————————————————

API中已經說明了BufferedWriter可以提高寫入字元流的效率。對於耗時的寫入操作,使用BufferWriter對象是非常明智的選擇。

下面是BufferedWriter的源碼。

package java.io;public class BufferedWriter extends Writer {    private Writer out;    private char cb[];    private int nChars, 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"));    }    private void ensureOpen() throws IOException {        if (out == null)            throw new IOException("Stream closed");    }    void flushBuffer() throws IOException {        synchronized (lock) {            ensureOpen();            if (nextChar == 0)                return;            out.write(cb, 0, nextChar);            nextChar = 0;        }    }        public void write(int c) throws IOException {        synchronized (lock) {            ensureOpen();            if (nextChar >= nChars)                flushBuffer();            cb[nextChar++] = (char) c;        }    }    private int min(int a, int b) {        if (a < b) return a;        return b;    }       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;            }            if (len >= nChars) {                /* If the request length exceeds the size of the output buffer,                   flush the buffer and then write the data directly.  In this                   way buffered streams will cascade harmlessly. */                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();            }        }    }       public void write(String s, int off, int len) throws IOException {        synchronized (lock) {            ensureOpen();            int b = off, t = off + len;            while (b < t) {                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();        }    }    @SuppressWarnings("try")    public void close() throws IOException {        synchronized (lock) {            if (out == null) {                return;            }            try (Writer w = out) {                flushBuffer();            } finally {                out = null;                cb = null;            }        }    }}

好像沒有新穎之處。。。。。應該是我道行太太太淺,看不出= =

可以看到BufferedWriter之所以叫做buffered,是因為,裡面使用了一個char 類型的數組作為緩衝(private char cb[];)。

寫操作的時候不會立刻寫入當前的字元流,而是先寫入緩衝區。

 

java.io.BufferedWriter API 以及源碼解讀

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.