zoukankan      html  css  js  c++  java
  • properties.load(in); 引出的中文乱码问题

                                                                                                                                                                  

    /**
    * Reads a property list (key and element pairs) from the input byte stream.
    * The input stream is in a simple line-oriented format and is assumed to use the ISO 8859-1 character encoding;
    * that is each byte is one Latin1 character.
    * Characters not in Latin1, and certain special characters,
    * are represented in keys and elements using Unicode escapes as defined in
    * section 3.3 of <cite>The Java Language Specification</cite>.
    * The specified stream remains open after this method returns.
    */

    load(InputStream inStream)

    以上我们注意到一下几点:

    1. 从byte流中读取键值对

    2. 流应该是规范的properties文件,流被假定为ISO 8859-1 字符编码(即流编码为gbk时会产生乱码)

    3. 1byte对应8bit,即二进制 1111 1111 ,表示范围是十六进制 00-FF

    4. 默认字符之外的字符应该使用Unicode 逃脱(eclipse properties 自带该功能),例如:小明 → u5c0fu660e

    5. 该方法执行完毕后,流依旧开着,需要调用close 将流关闭

                                                                                                                                                                   

    /**
    * Reads a property list (key and element pairs) from the input character stream in a simple line-oriented format.
    */

    load(Reader reader)

    以上我们注意到以下几点:

    1. 从character流中读取键值对

    2. 流应该是规范的properties文件

    3. input character stream 是可以读取中文的

                                                                                                                                                                   

    乱码解决方案一:

     using Unicode escapes

    乱码解决方案二:

    将InputStream 转换成 Reader

    import java.io.InputStreamReader;
    properties.load(new InputStreamReader(in));
  • 相关阅读:
    Vue学习手记01-安装和项目创建
    [Powershell] FTP Download File
    [PowerShell] Backup Folder and Files Across Network
    SSRS 请求并显示SharePoint人员和组字段
    Beta 冲刺 (2/7)
    Beta 冲刺 (1/7)
    BETA 版冲刺前准备
    事后诸葛亮
    Alpha 答辩总结
    α冲刺 (10/10)
  • 原文地址:https://www.cnblogs.com/zno2/p/4613401.html
Copyright © 2011-2022 走看看