1. 程式人生 > >thinking in java (三十一) ----- IO之 BufferedWriter

thinking in java (三十一) ----- IO之 BufferedWriter

介紹

BufferedWriter 是緩衝字元輸出流。它繼承於Writer。
BufferedWriter 的作用是為其他字元輸出流新增一些緩衝功能。

原始碼分析

package java.io;

public class BufferedWriter extends Writer {

    // 輸出流物件
    private Writer out;

    // 儲存“緩衝輸出流”資料的字元陣列
    private char cb[];

    // nChars 是cb緩衝區中字元的總的個數
    // nextChar 是下一個要讀取的字元在cb緩衝區中的位置
    private int nChars, nextChar;

    // 預設字元緩衝區大小
    private static int defaultCharBufferSize = 8192;

    // 行分割符
    private String lineSeparator;

    // 建構函式,傳入“Writer物件”,預設緩衝區大小是8k
    public BufferedWriter(Writer out) {
        this(out, defaultCharBufferSize);
    }

    // 建構函式,傳入“Writer物件”,指定緩衝區大小是sz
    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”是開啟狀態
    private void ensureOpen() throws IOException {
        if (out == null)
            throw new IOException("Stream closed");
    }

    // 對緩衝區執行flush()操作,將緩衝區的資料寫入到Writer中
    void flushBuffer() throws IOException {
        synchronized (lock) {
            ensureOpen();
            if (nextChar == 0)
                return;
            out.write(cb, 0, nextChar);
            nextChar = 0;
        }
    }

    // 將c寫入到緩衝區中。先將c轉換為char,然後將其寫入到緩衝區。
    public void write(int c) throws IOException {
        synchronized (lock) {
            ensureOpen();
            // 若緩衝區滿了,則清空緩衝,將緩衝資料寫入到輸出流中。
            if (nextChar >= nChars)
                flushBuffer();
            cb[nextChar++] = (char) c;
        }
    }

    // 返回a,b中較小的數
    private int min(int a, int b) {
        if (a < b) return a;
        return b;
    }

    // 將字元陣列cbuf寫入到緩衝中,從cbuf的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;
            }

            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();
            }
        }
    }

    // 將字串s寫入到緩衝中,從s的off位置開始寫入,寫入長度是len。
    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();
        }
    }

    public void close() throws IOException {
        synchronized (lock) {
            if (out == null) {
                return;
            }
            try {
                flushBuffer();
            } finally {
                out.close();
                out = null;
                cb = null;
            }
        }
    }
}

BufferedWriter的原始碼非常簡單,這裡就BufferedWriter的思想進行簡單說明:BufferedWriter通過字元陣列來緩衝資料,當緩衝區滿或者使用者呼叫flush()函式時,它就會將緩衝區的資料寫入到輸出流中。

示例程式碼

import java.io.BufferedWriter;
import java.io.File;
import java.io.OutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.lang.SecurityException;
import java.util.Scanner;

/**
 * BufferedWriter 測試程式
 *
 * @author skywang
 */
public class BufferedWriterTest {

    private static final int LEN = 5;
    // 對應英文字母“abcdefghijklmnopqrstuvwxyz”
    //private static final char[] ArrayLetters = "abcdefghijklmnopqrstuvwxyz";
    private static final char[] ArrayLetters = new char[] {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

    public static void main(String[] args) {
        testBufferedWriter() ;
    }

    /**
     * BufferedWriter的API測試函式
     */
    private static void testBufferedWriter() {

        // 建立“檔案輸出流”對應的BufferedWriter
        // 它對應緩衝區的大小是16,即緩衝區的資料>=16時,會自動將緩衝區的內容寫入到輸出流。
        try {
            File file = new File("bufferwriter.txt");
            BufferedWriter out =
                  new BufferedWriter(
                      new FileWriter(file));

            // 將ArrayLetters陣列的前10個字元寫入到輸出流中
            out.write(ArrayLetters, 0, 10);
            // 將“換行符\n”寫入到輸出流中
            out.write('\n');

            out.flush();
            //readUserInput() ;

            out.close();
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       } catch (SecurityException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }
    }

    /**
     * 讀取使用者輸入
     */
    private static void readUserInput() {
        System.out.println("please input a text:");
        Scanner reader=new Scanner(System.in);
        // 等待一個輸入
        String str = reader.next();
        System.out.printf("the input is : %s\n", str);
    }
}

執行結果
生成檔案“bufferwriter.txt”,檔案的內容是“abcdefghij”。

 

原文:http://www.cnblogs.com/skywang12345/p/io_24.html