轉自:http://www.fengfly.com/plus/view-214071-1.html
InputStreamReader和OutputStreamWriter 是位元組流通向字元流的橋梁:它使用指定的 charset 讀寫位元組并将其解碼為字元。
InputStreamReader 的作用是将“位元組輸入流”轉換成“字元輸入流”。它繼承于Reader。
OutputStreamWriter 的作用是将“位元組輸出流”轉換成“字元輸出流”。它繼承于Writer。
public class InputStreamReader extends Reader {
private final StreamDecoder sd;
// 根據in建立InputStreamReader,使用預設的編碼
public InputStreamReader(InputStream in) {
super(in);
try {
sd = StreamDecoder.forInputStreamReader(in, this, (String)null); // ## check lock object
} catch (UnsupportedEncodingException e) {
// The default encoding should always be available
throw new Error(e);
}
}
// 根據in建立InputStreamReader,使用編碼charsetName(編碼名)
public InputStreamReader(InputStream in, String charsetName)
throws UnsupportedEncodingException
{
super(in);
if (charsetName == null)
throw new NullPointerException("charsetName");
sd = StreamDecoder.forInputStreamReader(in, this, charsetName);
}
// 根據in建立InputStreamReader,使用編碼cs
public InputStreamReader(InputStream in, Charset cs) {
super(in);
if (cs == null)
throw new NullPointerException("charset");
sd = StreamDecoder.forInputStreamReader(in, this, cs);
}
// 根據in建立InputStreamReader,使用解碼器dec
public InputStreamReader(InputStream in, CharsetDecoder dec) {
super(in);
if (dec == null)
throw new NullPointerException("charset decoder");
sd = StreamDecoder.forInputStreamReader(in, this, dec);
}
// 擷取解碼器
public String getEncoding() {
return sd.getEncoding();
}
// 讀取并傳回一個字元
public int read() throws IOException {
return sd.read();
}
// 将InputStreamReader中的資料寫入cbuf中,從cbuf的offset位置開始寫入,寫入長度是length
public int read(char cbuf[], int offset, int length) throws IOException {
return sd.read(cbuf, offset, length);
}
// 能否從InputStreamReader中讀取資料
public boolean ready() throws IOException {
return sd.ready();
}
// 關閉InputStreamReader
public void close() throws IOException {
sd.close();
}
}
public class OutputStreamWriter extends Writer {
private final StreamEncoder se;
// 根據out建立OutputStreamWriter,使用編碼charsetName(編碼名)
public OutputStreamWriter(OutputStream out, String charsetName)
throws UnsupportedEncodingException
{
super(out);
if (charsetName == null)
throw new NullPointerException("charsetName");
se = StreamEncoder.forOutputStreamWriter(out, this, charsetName);
}
// 根據out建立OutputStreamWriter,使用預設的編碼
public OutputStreamWriter(OutputStream out) {
super(out);
try {
se = StreamEncoder.forOutputStreamWriter(out, this, (String)null);
} catch (UnsupportedEncodingException e) {
throw new Error(e);
}
}
// 根據out建立OutputStreamWriter,使用編碼cs
public OutputStreamWriter(OutputStream out, Charset cs) {
super(out);
if (cs == null)
throw new NullPointerException("charset");
se = StreamEncoder.forOutputStreamWriter(out, this, cs);
}
// 根據out建立OutputStreamWriter,使用編碼器enc
public OutputStreamWriter(OutputStream out, CharsetEncoder enc) {
super(out);
if (enc == null)
throw new NullPointerException("charset encoder");
se = StreamEncoder.forOutputStreamWriter(out, this, enc);
}java io系列01之 "目錄"
// 擷取編碼器enc
public String getEncoding() {
return se.getEncoding();
}
// 重新整理緩沖區
void flushBuffer() throws IOException {
se.flushBuffer();
}
// 将單個字元寫入到OutputStreamWriter中
public void write(int c) throws IOException {
se.write(c);
}
// 将字元數組cbuf從off開始的資料寫入到OutputStreamWriter中,寫入長度是len
public void write(char cbuf[], int off, int len) throws IOException {
se.write(cbuf, off, len);
}
// 将字元串str從off開始的資料寫入到OutputStreamWriter中,寫入長度是len
public void write(String str, int off, int len) throws IOException {
se.write(str, off, len);
}java io系列01之 "目錄"
// 重新整理“輸出流”
// 它與flushBuffer()的差別是:flushBuffer()隻會重新整理緩沖,而flush()是重新整理流,flush()包括了flushBuffer。
public void flush() throws IOException {
se.flush();
}
// 關閉“輸出流”
public void close() throws IOException {
se.close();
}
}
說明:
OutputStreamWriter 作用和原理都比較簡單。
作用就是将“位元組輸出流”轉換成“字元輸出流”。它的原理是,我們建立“字元輸出流”對象時,會指定“位元組輸出流”以及“字元編碼”。
示例見原文