zno2

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 自带该功能),例如:小明 → \u5c0f\u660e

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));

 

posted on 2016-08-05 18:38  zno2  阅读(1750)  评论(0编辑  收藏  举报

导航