InputStreamReader,OutputStreamWriter

InputStreamReader


InputStreamReader作用就是将“字节输入流”转换成“字符输入流”

输入流译码器

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

OutputStreamWriter


OutputStreamWriter作用就是将“字节输出流”转换成“字符输出流”

输出流译码器

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

获取编码器

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

刷新输出流,与flushBuffer()的区别是:flushBuffer()只会刷新缓冲区,而flush()是刷新流,flush()包括了flushBuffer。

    public void flush() throws IOException {
        se.flush();
    }

关闭OutputStreamWriter

    public void close() throws IOException {
        se.close();
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值