Java Reader/Writer

Java讀取器和寫入器(Reader/Writer)是基於字符的流。當要從數據源讀取基於字符的數據時,使用讀取器(Reader)。當想要寫入基於字符的數據時使用寫入器(Writer)。

如果有一個流提供字節,想通過將這些字節解碼爲字符讀取字符,應該使用InputStreamReader類。

例如,如果有一個名爲isoInputStream對象,並且想要獲取一個Reader對象實例,那麼可以這樣做:

Reader  reader = new InputStreamReader(iso);

如果知道在基於字節的流中使用的編碼,可在創建Reader對象時指定它,如下所示:

Reader  reader = new InputStreamReader(iso,  "US-ASCII");

類似地,可以創建一個Writer對象,從基於字節的輸出流中輸出的字符,如下所示,假設oso是一個OutputStream對象。以下代碼使從OutputStream使用平臺默認編碼創建Writer對象。

Writer writer  = new OutputStreamWriter(oso);

使用「US-ASCII」編碼從OutputStream創建Writer對象

Writer writer  = new OutputStreamWriter(oso,  "US-ASCII");

示例 -

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

public class Main {
  public static void main(String[] args) {
    String destFile = "test.txt";

    try (BufferedWriter bw = new BufferedWriter(new FileWriter(destFile))) {
      bw.append("test");
      bw.newLine();
      bw.append("test1");
      bw.newLine();
      bw.append("test2");
      bw.newLine();
      bw.append("test3");

      bw.flush();
    }  catch (Exception e2) {
      e2.printStackTrace();
    }
  }
}

以下代碼從test.txt文件中讀取文本。

import java.io.BufferedReader;
import java.io.FileReader;

public class Main {
  public static void main(String[] args) throws Exception{
    String srcFile = "test.txt";
    BufferedReader br = new BufferedReader(new FileReader(srcFile));
    String text = null;

    while ((text = br.readLine()) != null) {
      System.out.println(text);
    }
    br.close();
  }
}

將基於字節的流轉換爲基於字符的流是直接的。如果有一個InputStream對象,可以通過將它包裝在一個InputStreamReader對象中來獲得一個Reader對象,如下所示:

InputStream is = create your  InputStream object here; 
Reader  reader = new InputStreamReader(is);

要從InputStream對象構造BufferedReader對象,那麼可以這樣做:

InputStream is = create your  InputStream object here; 
BufferedReader br  = new BufferedReader(new InputStreamReader(is));

可以從OutputStream對象構造一個Writer對象,如下所示:

OutputStream os  = create your  OutputStream object here; Writer writer  = new OutputStreamWriter(os);

示例

import java.io.FileReader;
import java.io.BufferedReader;
import java.io.Reader;
import java.io.FilterReader;
import java.io.IOException;

class LowerCaseReader extends FilterReader {
    public LowerCaseReader(Reader in) {
        super(in);
    }

    @Override
    public int read(char[] cbuf, int off, int len) throws IOException {
        int count = super.read(cbuf, off, len);
        if (count != -1) {
            // Convert all read characters to lowercase
            int limit = off + count;
            for (int i = off; i < limit; i++) {
                cbuf[i] = Character.toLowerCase(cbuf[i]);
            }
        }
        return count;
    }
}

public class Main {
    public static void main(String[] args) throws Exception {
        String fileName = "test.txt";
        LowerCaseReader lcr = new LowerCaseReader(new FileReader(fileName));
        int c = -1;
        while ((c = lcr.read()) != -1) {
            System.out.print((char) c);
        }
        lcr.close();
        BufferedReader br = new BufferedReader(new LowerCaseReader(new FileReader(fileName)));
        String str = null;
        while ((str = br.readLine()) != null) {
            System.out.println(str);
        }
        br.close();
    }
}

執行上面代碼,得到以下結果 -

Yiibai.com BufferedInputStream Usge.
The Line 2..yiibai.com bufferedinputstream usge.
the line 2..