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));
  • 相关阅读:
    中国机读目录格式(CNMARC)
    smarty截取中文字符乱码问题?gb2312utf8
    smarty模板截取字符串乱码问题完美解决```````
    marc数据
    smarty精品教程三(高级篇)
    中国机读目录格式(CNMARC)附录
    smarty精品教程二(高级篇)
    QuickSkin简明教程
    smarty精品教程四(高级篇)可下载实例
    maven学习指南
  • 原文地址:https://www.cnblogs.com/zno2/p/4613401.html
Copyright © 2011-2022 走看看