快捷搜索:  as  2018  FtCWSyGV  С˵  test  xxx  Ψһ  w3viyKQx

澳门新葡亰平台app:Java io 入门(二十一)InputStreamReader和OutputStreamWriter源码分析及介绍使用



InputStreamReader和OutputStreamWriter 是字撙节通向字符流的桥梁:它应用指定的 charset 读写字节并将其解码为字符。

InputStreamReader 的感化是将“字节输入流”转换成“字符输入流”。它承袭于Reader。

OutputStreamWriter 的感化是将“字节输出流”转换成“字符输出流”。它承袭于Writer。

InputStreamReader和Outp澳门新葡亰平台apputStreamWriter源码阐发

1. InputStreamReader 源码(基于jdk1.7.40)

package java.io;

import java.nio.charset.Charset;

import java.nio.charset.CharsetDecoder;import sun.nio.cs.StreamDecoder;

// 将“字节输入流”转换成“字符输入流”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 encodi澳门新葡亰平台appng 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,应用编码cspublic InputStreamReader(InputStream in, Charset cs) {

super(in);if (cs == null)

throw new NullPointerException("charset");sd = StreamDecoder.forInputStreamReader(in, this, cs);

}

// 根据in创建InputStreamReader,应用解码器decpublic 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位置开始写入,写入长度是lengthpublic int read(char cbuf[], int offset, int澳门新葡亰平台app length) throws IOException {

return sd.read(cbuf, offset, length);}

// 能否从InputStreamReader中读取数据

public boolean ready() throws IOException {return sd.ready();

}

// 关闭InputStreamReaderpublic void close() throws IOException {

sd.close();}

}

2. OutputStreamWriter 源码(基于jdk1.7.40)

package java.io;

import java.nio.charset.Charset;

import java.nio.charset.CharsetEncoder;import sun.nio.cs.StreamEncoder;

// 将“字节输出流”转换成“字符输出流”

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,应用编码cspublic OutputStreamWriter(OutputStream out, Charset cs) {

super(out);if (cs == null)

throw new NullPointerException("charset");se = StreamEncoder.forOutputStreamWriter(out, this, cs);

}

// 根据out创建OutputStreamWriter,应用编码器encpublic OutputStreamWriter(OutputStream out, CharsetEncoder enc) {

super(out);if (enc == null)

throw new NullPointerException("charset encod澳门新葡亰平台apper");se = StreamEncoder.forOutputStreamWriter(out, this, enc);

}java io系列01之 "目录"

// 获取编码器encpublic String getEncoding() {

return se.getEncoding();}

// 刷新缓冲区

void f澳门新葡亰平台applushBuffer() 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中,写入长度是lenpublic 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 感化和道理都对照简单。

感化便是将“字节输出流”转换成“字符输出流”。它的道理是,我们创建“字符输出流”工具时,会指定“字节输出流”以及“字符编码”。

您可能还会对下面的文章感兴趣: