1. 程式人生 > 其它 >JAVA篇:Java IO (七) 字串流和陣列流

JAVA篇:Java IO (七) 字串流和陣列流

7、字串流 StringReader和StringWriter

字串流的源是一個字串,其讀取以及寫入是按照字元char來進行的。

7.1 StringReader

StringReader在構造時會傳入一個字串作為源,並且支援mark方法

    private String str;
    private int length;
    private int next = 0;//指向下一個可讀取的位置
    private int mark = 0;//"書籤"
   /*建構函式*/
    public StringReader(String s) {
        this.str = s;
        
this.length = s.length(); }

讀取時按照字元讀取。

    public int read() throws IOException {
        synchronized (lock) {
            ensureOpen();
            if (next >= length)
                return -1;
            return str.charAt(next++);
        }
    }
​
    public int read(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 0; } if (next >= length) return -1; int n = Math.min(length - next, len); str.getChars(next, next + n, cbuf, off); next += n; return n; } }

7.2 StringWriter

StringWriter則可以自定義字串緩衝區大小或者使用使用預設大小。其close()方法無效,關閉後仍可呼叫而不會丟擲異常。

    private StringBuffer buf;/*字串緩衝區*/public StringWriter() {
        buf = new StringBuffer();
        lock = buf;
    }
​
    public StringWriter(int initialSize) {
        if (initialSize < 0) {
            throw new IllegalArgumentException("Negative buffer size");
        }
        buf = new StringBuffer(initialSize);
        lock = buf;
    }

StringWriter包含各種writer和append方法,而且可以使用toString()和getBuffer()獲取已經寫入的資料。

8、陣列流

如果說檔案流是把檔案當做資料來源,那麼陣列流則是把記憶體中的某個位元組/字元陣列物件當做資料來源,檔案流包含位元組陣列輸入流ByteArrayInputStream和位元組陣列輸出流ByteArrayOutputStream,字元陣列輸入流CharArrayReader和字元陣列輸出流CharArrayWriter。

8.1 位元組陣列輸入流ByteArrayInputStream

java.io.ByteArrayInputStream包含一個內部緩衝區,該緩衝區包含從流中讀取的位元組。內部計數器跟蹤read方法要提供的下一個位元組。

    /*內部緩衝區*/
    protected byte buf[];
    /*內部計數器*/
    protected int pos;

需要注意的是,關閉ByteArrayInputStream無效。此類中的方法在關閉流後仍可被呼叫而不會產生任何IOException。這個可以從原始碼中看出來,

其建構函式只是將傳入的位元組陣列傳入到內部緩衝區,在讀取的時候從緩衝區讀取。而該流的close()方法什麼都沒有做。

    /**
     * Closing a <tt>ByteArrayInputStream</tt> has no effect. The methods in
     * this class can be called after the stream has been closed without
     * generating an <tt>IOException</tt>.
     */
    public void close() throws IOException {
    }

8.2 位元組陣列輸出流ByteArrayOutputStream

java.io.ByteArrayOutputStream實現了一個輸出流,其中的資料被寫入一個byte陣列,緩衝區會隨著資料的不斷寫入而自動增長,可使用toByteArray()和toString()獲取資料。同樣的,關閉ByteArrayOutputStream無效。

    /*內部緩衝區*/
    protected byte buf[];
    /*緩衝區資料個數*/
    protected int count;

相比於位元組陣列輸入流,位元組陣列輸出流做了什麼似乎就沒那麼明確了。

位元組陣列輸入流ByteArrayInputStream做的是將一個位元組陣列封裝進流中,然後我們通過位元組陣列輸入流來讀取該位元組陣列的資料。

而位元組陣列輸出流ByteArrayOutputStream做的是,建立ByteArrayOutputStream例項的時候只建立了預設或指定大小的內部緩衝區,然後通過write方法將資料寫入內部緩衝區(期間緩衝區會自動增長),可通過toString()和toByteArray()獲取流中的資料,或者使用writeTo(OutputStream out)將資料寫入到指定的輸出流中。

8.3 字元陣列輸入流CharArrayReader

java.io.CharArrayReader實現一個可用作字元輸入流的字元緩衝區。

    /** The character buffer. */
    protected char buf[];
​
    /** The current buffer position. */
    protected int pos;
​
    public CharArrayReader(char buf[]) {
        this.buf = buf;
        this.pos = 0;
        this.count = buf.length;
    }
​
    public CharArrayReader(char buf[], int offset, int length) {
        if ((offset < 0) || (offset > buf.length) || (length < 0) ||
            ((offset + length) < 0)) {
            throw new IllegalArgumentException();
        }
        this.buf = buf;
        this.pos = offset;
        this.count = Math.min(offset + length, buf.length);
        this.markedPos = offset;
    }

只是其close()會將緩衝區置空,關閉流之後不可呼叫。

public void close() {
        buf = null;
    }

8.4 字元陣列輸出流CharArrayWriter

java.io.CharArrayWriter實現一個可用作Writer的字元緩衝區。緩衝區會隨向流中寫入資料而自動增長。可使用toCharArray()和toString()獲取資料。

該類的close()方法也是無效的,並未做任何操作。

    /**
     * Close the stream.  This method does not release the buffer, since its
     * contents might still be required. Note: Invoking this method in this class
     * will have no effect.
     */
    public void close() { }

當你深入瞭解,你就會發現世界如此廣袤,而你對世界的瞭解則是如此淺薄,請永遠保持謙卑的態度。