[重学Java基础][Java IO流][Part.6]字节-字符转换输入流输出流

[重学Java基础][JavaIO流][Part.6]字节-字符转换输入流输出流

InputStramReader

概述

InputStramReader是一个转换器类 包装流 用于将字节输入流InputStream转换为字符输入流
此类应用了复合的设计方法 功能大部分通过转发内部对象StreamDecoder 实现

官方注释

InputStramReader类是从byte流转换到字符流的桥梁

该类读取byte字符并使用java.nio.charset.Charset字符集进行解码

或者字符集可根据名称指定或者显式指定,否则使用平台默认字符集

每一次对该类read方法调用的时候都会从底层流读入一个或多个字节

要有效的将字节转化为字符格式,可能需要提前读入更多的字节来满足最低转化要求

为了达到最高效率,可以用BufferedReader来包装InputStreamReader

源码解析

成员变量

 private final StreamDecoder sd

成员变量只有一个 就是一个底层nio流 StreamDecoder

这里写图片描述

成员方法

构造方法 必须传入一个InputStream作为数据源 构造函数本质上是创建了StreamDecoder对象

使用默认字符集创建StreamDecoder对象
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);
    }
}
根据名字指定字符集创建StreamDecoder对象
public InputStreamReader(InputStream in, String charsetName)
    throws UnsupportedEncodingException
{
    super(in);
    if (charsetName == null)
        throw new NullPointerException("charsetName");
    sd = StreamDecoder.forInputStreamReader(in, this, charsetName);
}
根据直接指定的字符集对象创建StreamDecoder对象
public InputStreamReader(InputStream in, Charset cs) {
    super(in);
    if (cs == null)
        throw new NullPointerException("charset");
    sd = StreamDecoder.forInputStreamReader(in, this, cs);
}
根据直接指定的字符集编码器对象(实质上还是字符集)创建StreamDecoder对象
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();
    }

指定读入字符数组大小 游标位置
public int read(char cbuf[], int offset, int length) throws IOException {
    return sd.read(cbuf, offset, length);
}

可以看到 InputSteamReader的方法基本上全部都是转发StreamDecoder的方法
所以我们还是看一下StreamDecoder的源码

StreamDecoder源码解析

代码示例

    InputStreamReader isr=new InputStreamReader(System.in);

    BufferedReader br=new BufferedReader(isr);

    String str=br.readLine();

    System.out.println("str="+str);

输出结果

8848asdf
str=8848asdf

OutputStreamWriter

概述

OutputStreamWriter是一个转换器类 包装流 用于将字节输入流OutputStream转换为字符输入流
此类应用了复合的设计方法 功能大部分通过转发内部对象StreamEncoder实现

官方注释

OutputStreamWriter类是从byte流转换到字符流的桥梁
该类写入byte字符并使用给定的字符集进行解码,或者字符集可根据名称指定或者显式指定
否则使用平台默认字符集。每一次对该类write方法调用的时候都会使用编码转换器转换写入的字符。在真正写入到下层输出流之前,写入的字符内容都会保存在一个缓冲区中。缓冲区的大小是可以指定的,但绝大多数情况下已经满足使用需求。

要有效的将字节转化为字符格式,可能需要提前读入更多的字节来满足最低转化要求
为了达到最高效率,可以用BufferedReader来包装InputStreamReader

源码解析

成员变量

只有一个StreamEncoder

private final StreamEncoder se;

成员方法

构造方法 类似InputStreamReader 都是不同参数重载 区别在字符集

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) {
    super(out);
    try {
        se = StreamEncoder.forOutputStreamWriter(out, this, (String)null);
    } catch (UnsupportedEncodingException e) {
        throw new Error(e);
    }
}

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

主要方法 写入方法 全是转发Streamencoder的方法

public void write(int c) throws IOException {
    se.write(c);
}

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

所以我们还是看一下Streamencoder的源码
Streamencoder的源码

代码示例

    FileOutputStream fous=new FileOutputStream("D://fous.txt");

    OutputStreamWriter osw=new OutputStreamWriter(fous);

    BufferedWriter bw=new BufferedWriter(osw);

    String str="为美好的世界献上祝福";
    bw.write(str,0,str.length());
    bw.close();

运行结果
这里写图片描述

猜你喜欢

转载自blog.csdn.net/u011863951/article/details/80004622