• 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));
  • 相关阅读:
    Duilib学习笔记《04》— 窗体显示
    Duilib学习笔记《03》— 控件使用
    Duilib学习笔记《02》— 界面布局
    Duilib扩展《01》— 双击、右键消息扩展
    Duilib学习笔记《01》— duilib整体框架认识
    C++中rapidxml用法及例子
    C++ Namespace 详解
    Sencha touch navigation 内嵌list,itemTap第二次点击不跳转的问题
    Webapp meta标签解决移动缩放的问题
    (转载)Web存储和SessionStorage locaStorage
  • 原文地址:https://www.cnblogs.com/zno2/p/4613401.html
Copyright © 2020-2023  润新知