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

 

转载于:https://www.cnblogs.com/zno2/p/4613401.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值