转换流(处理流的一种,属于字符流)

6 篇文章 1 订阅

目录

一、概述

1.转换流的作用

2.种类

①INputStreamReader

②OutputStreamReader

图示

3.编码&解码

①解码

②编码

4.字符集

1.字符编码的由来及常见的编码表

2.UTF-8和Unicode编码的区别

3.Unicode编码

二、INputStreamReader的使用

三、OutputStreamReader的使用


一、概述

1.转换流的作用

提供了在字节流和字符流之间的转换

字节流中的数据都是字符时,转成字符流操作更高效

2.种类

INputStreamReader

将InputStream转换为Reader(将一个字节的输入流转换为字符的输入流)

OutputStreamReader

将Writer转换为OutputStream(将一个字符的输出流转换为字节的输出流)

图示

3.编码&解码

①解码

字节、字节数组 ——> 字符数组、字符串

②编码

字符数组、字符串 ——> 字节、字节数组

4.字符集

1.字符编码的由来及常见的编码表

2.UTF-8和Unicode编码的区别

3.Unicode编码

Unicode只是定义了一个庞大、全球通用的字符集,并为每个字符规定了唯一确定的编号,具体存储成什么样的字节流,取决于字符编码方案

二、INputStreamReader的使用

    @Test
    public void test1() {
        InputStreamReader isr = null;
        try {
            FileInputStream fis = new FileInputStream("abab");
//        InputStreamReader isr = new InputStreamReader(fis);//使用系统默认的字符集
            //指明了字符集:具体使用哪个字符集,取决于文件abab保存时使用的字符集
            isr = new InputStreamReader(fis,"UTF-8");
            char[] cbuf = new char[1024];
            int len;
            while ((len = isr.read(cbuf)) != -1){
                String str = new String(cbuf,0,len);
                System.out.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(isr != null){
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

三、OutputStreamReader的使用

综合使用INputStreamReaderOutputStreamReader

    @Test
    public void test2(){
        InputStreamReader isr = null;
        OutputStreamWriter osw = null;
        try {
            File file1 = new File("abab");
            File file2 = new File("abab_gbk");

            FileInputStream fis = new FileInputStream(file1);
            FileOutputStream fos = new FileOutputStream(file2);

            isr = new InputStreamReader(fis,"utf-8");
            osw = new OutputStreamWriter(fos,"gbk");
            //读写过程
            char[] cbuf = new char[1024];
            int len;
            while ((len = isr.read(cbuf)) != -1){
                osw.write(cbuf,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(isr != null){
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(osw != null){
                try {
                    osw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

elk-zhang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值