zoukankan      html  css  js  c++  java
  • 转换流:InputStreamReader、OutputStreamWriter

    
    

    1.转换流涉及到的类:属于字符流
    InputStreamReader:将一个字节的输入流转换为字符的输入流
    解码:字节、字节数组 --->字符数组、字符串

    
    

    OutputStreamWriter:将一个字符的输出流转换为字节的输出流
    编码:字符数组、字符串 ---> 字节、字节数组

    
    

    说明:编码决定了解码的方式

    
    


    2.作用:提供字节流与字符流之间的转换

    
    

    3.图示:


    
    
    



    /**

    * 处理流之二:转换流的使用
    * 1.转换流:属于字符流
    * InputStreamReader:将一个字节的输入流转换为字符的输入流
    * OutputStreamWriter:将一个字符的输出流转换为字节的输出流
    *
    * 2.作用:提供字节流与字符流之间的转换
    *
    * 3. 解码:字节、字节数组 --->字符数组、字符串
    * 编码:字符数组、字符串 ---> 字节、字节数组
    *
    *
    * 4.字符集
    *ASCII:美国标准信息交换码。
    用一个字节的7位可以表示。
    ISO8859-1:拉丁码表。欧洲码表
    用一个字节的8位表示。
    GB2312:中国的中文编码表。最多两个字节编码所有字符
    GBK:中国的中文编码表升级,融合了更多的中文文字符号。最多两个字节编码
    Unicode:国际标准码,融合了目前人类使用的所有字符。为每个字符分配唯一的字符码。所有的文字都用两个字节来表示。
    UTF-8:变长的编码方式,可用1-4个字节来表示一个字符。

    *
    *
    * @author CH
    * @create 2021 下午 4:25
    */


    /*
        此时处理异常的话,仍然应该使用try-catch-finally
        InputStreamReader的使用,实现字节的输入流到字符的输入流的转换
         */
        @Test
        public void test1() throws IOException {
    
            FileInputStream fis = new FileInputStream("dbcp.txt");
    //        InputStreamReader isr = new InputStreamReader(fis);//使用系统默认的字符集
            //参数2指明了字符集,具体使用哪个字符集,取决于文件dbcp.txt保存时使用的字符集
            InputStreamReader isr = new InputStreamReader(fis,"UTF-8");//使用系统默认的字符集
    
            char[] cbuf = new char[20];
            int len;
            while((len = isr.read(cbuf)) != -1){
                String str = new String(cbuf,0,len);
                System.out.print(str);
            }
    
            isr.close();
    
        }

    /*
        此时处理异常的话,仍然应该使用try-catch-finally
    
        综合使用InputStreamReader和OutputStreamWriter
         */
        @Test
        public void test2() throws Exception {
            //1.造文件、造流
            File file1 = new File("dbcp.txt");
            File file2 = new File("dbcp_gbk.txt");
    
            FileInputStream fis = new FileInputStream(file1);
            FileOutputStream fos = new FileOutputStream(file2);
    
            InputStreamReader isr = new InputStreamReader(fis,"utf-8");
            OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");
    
            //2.读写过程
            char[] cbuf = new char[20];
            int len;
            while((len = isr.read(cbuf)) != -1){
                osw.write(cbuf,0,len);
            }
    
            //3.关闭资源
            isr.close();
            osw.close();
    
    
        }
    不积跬步,无以至千里;不积小流,无以成江海。
  • 相关阅读:
    javaweb 安全传输签名机制
    GC详解及Minor GC和Full GC触发条件总结
    程序员的人性思考(上)
    LeetCode 845——数组中的最长山脉
    LeetCode 386——字典序的第 K 小数字
    LeetCode 386——字典序排数
    LeetCode 135——分发糖果
    Ubuntu 16.04 安装显卡驱动后循环登录和无法设置分辨率的一种解决方案
    在 Ubuntu 下安装 Deepin 的 QQ、微信、百度云和迅雷等软件
    腾讯 2019 春季暑期实习生提前批笔试——技术研究与数据分析
  • 原文地址:https://www.cnblogs.com/CCTVCHCH/p/14882137.html
Copyright © 2011-2022 走看看