zoukankan      html  css  js  c++  java
  • java 输入输出IO 转换流-字符编码

    编码和其产生的问题:

    计算机中储存的信息都是用二进制数表示的,而我们在屏幕上看到的数字、英文、标点符号、汉字等字符是二进制数转换之后的结果。
    按照某种规则,将字符存储到计算机中,称为编码 。反之,将存储在计算机中的二进制数按照某种规则解析显示出来,称为解码 。
    比如说,按照A规则存储,同样按照A规则解析,那么就能显示正确的文本符号。反之,按照A规则存储,再按照B规则解析,就会导致乱码现象。
    字符集和编码的关系参考:https://www.cnblogs.com/zhangmingda/p/9030229.html 理解。
    在IDEA中,使用 FileReader 读取项目中的文本文件。由于IDEA的设置,都是默认的 UTF-8 编码,所以没有任何问题。但是,当读取Windows系统中创建的文本文件时,由于Windows系统的默认是GBK编码,就会出现乱码。
    如在windows上用记事本打开,随便写入一段中文:
    然后,我们用IO流FileReader读出来打印
    import java.io.*;
    
    /**
     * @ClassName CharSetTransferExample
     * @projectName: object1
     * @author: Zhangmingda
     * @description: XXX
     * date: 2021/4/18.
     */
    public class CharSetTransferExample {
        public static void main(String[] args) {
            String textPath = "输入输出文件读写/src/test/output/windows-create-剑门关.txt";
            String tmpLine;
            try(BufferedReader bufferedReader = new BufferedReader(new FileReader(textPath))) {
                while ((tmpLine = bufferedReader.readLine()) != null){
                    System.out.println(tmpLine);
                }
            }catch (FileNotFoundException e){
                e.printStackTrace();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

    输出结果:

    解决方案:通过转换流 指定字符编码 转换字节流如下↓↓↓

    1、InputStreamReader

    java.io.InputStreamReader ,是Reader的子类,是从字节流到字符流的桥梁。它读取字节,并使用指定的字符集将其解码为字符。它的字符集可以由名称指定,也可以接受平台的默认字符集。

    语法:

    • InputStreamReader(InputStream in): 创建一个使用默认字符集的字符流。
    • InputStreamReader(InputStream in, String charsetName): 创建一个指定字符集的字符流。
    import java.io.*;
    
    /**
     * @ClassName CharSetTransferExample
     * @projectName: object1
     * @author: Zhangmingda
     * @description: XXX
     * date: 2021/4/18.
     */
    public class CharSetTransferExample {
        public static void main(String[] args) {
            String textPath = "输入输出文件读写/src/test/output/windows-create-剑门关.txt";
            String tmpLine;
    //        try(BufferedReader bufferedReader = new BufferedReader(new FileReader(textPath))) { // 打开GBK编码文本,输出乱码
            try(BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(textPath),"GBK"))) {
                while ((tmpLine = bufferedReader.readLine()) != null){
                    System.out.println(tmpLine);
                    /**
                     * 剑门关-V哥
                     * 北望剑阁一线天,
                     * 遥想翼德战关前。
                     * 纵使王朝风云变,
                     * 雄关笑傲数千年。
                     */
                }
            }catch (FileNotFoundException e){
                e.printStackTrace();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

    2、OutStreamWriter

    import java.io.*;
    
    /**
     * @ClassName WriteTextOutputStreamCharSetTransferExample
     * @projectName: object1
     * @author: Zhangmingda
     * @description: XXX
     * date: 2021/4/18.
     */
    public class WriteTextOutputStreamCharSetTransferExample {
        public static void main(String[] args) {
            String dstTextPath = "输入输出文件读写/src/test/output/idea-write-gbkString.txt";
            try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dstTextPath),"GBK"))){
                bw.write("我好爱你,爱爱爱啊!");
                bw.flush();
            }catch (UnsupportedEncodingException e){
                e.printStackTrace();
            }catch (FileNotFoundException e){
                e.printStackTrace();
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    写入GBK编码字符成功,IDEA打提示当前默认使用UTF-8打开,建议用GBK编码reload

  • 相关阅读:
    nginx学习二:快速入门
    nginx学习一:http协议
    使用itext生成pdf的,各种布局
    关于java poi itext生成pdf文件的例子以及方法
    半透明全屏蒙层+全屏屏蔽+内容居中+css
    通过html文件生成PDF文件
    mybatis中文官网
    经典sql语句
    关于Cell中的各种值的类型判断
    bootstrap表格参数说明
  • 原文地址:https://www.cnblogs.com/zhangmingda/p/14673821.html
Copyright © 2011-2022 走看看