超详细的逐句介绍Java高级接口之文件输入/输出转换流函数InputStreamReader和OutputStreamWriter函数源码讲解(全)
作者:互联网
一、InputStreamReader和OutputStreamWriter
本专栏文章超详细的逐句介绍Java高级接口之文件输入/输出流函数FileReader和FileWriter函数源码讲解(全)的具体方法实现主要在文件输入/输出转换流函数InputStreamReader和OutputStreamWriter函数,下面我将从源码角度进行介绍。
二、OutputStreamWriter源码介绍
InputStreamReader继承于Writer
public class OutputStreamWriter extends Writer {
]
下面定义了流的编码规则
private final StreamEncoder se;
创建一个使用命名字符集的 OutputStreamWriter。
public OutputStreamWriter(OutputStream out, String charsetName)
throws UnsupportedEncodingException
{
super(out);
if (charsetName == null)
throw new NullPointerException("charsetName");
se = StreamEncoder.forOutputStreamWriter(out, this, charsetName);
}
使用命名字符集创建输出转换流函数
public OutputStreamWriter(OutputStream out, String charsetName)
throws UnsupportedEncodingException
{
super(out);
if (charsetName == null)
throw new NullPointerException("charsetName");
se = StreamEncoder.forOutputStreamWriter(out, this, charsetName);
}
创建一个使用默认字符编码的 OutputStreamWriter。
public OutputStreamWriter(OutputStream out) {
super(out);
try {
se = StreamEncoder.forOutputStreamWriter(out, this, (String)null);
} catch (UnsupportedEncodingException e) {
throw new Error(e);
}
}
创建一个使用给定字符集的 OutputStreamWriter。
public OutputStreamWriter(OutputStream out, Charset cs) {
super(out);
if (cs == null)
throw new NullPointerException("charset");
se = StreamEncoder.forOutputStreamWriter(out, this, cs);
}
按照指定的字符集合创建输出流函数
public OutputStreamWriter(OutputStream out, CharsetEncoder enc) {
super(out);
if (enc == null)
throw new NullPointerException("charset encoder");
se = StreamEncoder.forOutputStreamWriter(out, this, enc);
}
获取编码规则
public String getEncoding() {
return se.getEncoding();
}
定义缓冲区
void flushBuffer() throws IOException {
se.flushBuffer();
}
定义写函数(写入整型)
public void write(int c) throws IOException {
se.write(c);
}
定义写函数(char数组)
public void write(char cbuf[], int off, int len) throws IOException {
se.write(cbuf, off, len);
}
定义写入字符串的方法
public void write(String str, int off, int len) throws IOException {
se.write(str, off, len);
}
定义刷新函数
public void flush() throws IOException {
se.flush();
}
定义关闭函数
public void close() throws IOException {
se.close();
}
三、InputStreamReader函数介绍
InputStreamReader函数继承于Reader 类
public class InputStreamReader extends Reader {
}
定义流解码器
private final StreamDecoder sd;
用默认字符集创建文件输入流函数
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);
}
}
用指定字符集创建文件输入流函数
public InputStreamReader(InputStream in, String charsetName)
throws UnsupportedEncodingException
{
super(in);
if (charsetName == null)
throw new NullPointerException("charsetName");
sd = StreamDecoder.forInputStreamReader(in, this, charsetName);
}
创建一个使用给定字符集的 InputStreamReader。
public InputStreamReader(InputStream in, Charset cs) {
super(in);
if (cs == null)
throw new NullPointerException("charset");
sd = StreamDecoder.forInputStreamReader(in, this, cs);
}
用指定的解码器创建一个新的 InputStreamReader
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();
}
读取一个char数组
public int read(char cbuf[], int offset, int length) throws IOException {
return sd.read(cbuf, offset, length);
}
判断是否准备好了
public boolean ready() throws IOException {
return sd.ready();
}
定义流关闭函数
public void close() throws IOException {
sd.close();
}
标签:Java,函数,throws,源码,OutputStreamWriter,InputStreamReader,public,se,out 来源: https://blog.csdn.net/m0_43405302/article/details/121385780