note about property & charset

usually, I use property to rw data stead of database when it's simply.
but I offen run into an other trouble: hell of charset about chinese.
in fact, it's not only trouble about chinese, some espically character are so on. such as ' '(space), \n, \r, \t, <, >, ... etc.

there is 2 solution I found as below:
approach 1:
save your properties file as utf-8, utf-8 is a good solution for cross-platform & multi-language, but don't forget read it by utf-8 too.

It's error demo, it will get some chaos code stead chinese what you want

InputStream stream=new FileInputStream("demo.properties");
Property p=new Property;
p.load(stream)


the correct demo

InputStream stream=new FileInputStream("demo.properties");
InputStreamReader reader=new InputStreamReader(stream, "utf-8");
Property p=new Property;
p.load(reader)


approach 2:
though approach above are solved issue of chinese, but it not word about especial code (' ', \t, \r, \n, etc.)
so here is an other solution:

native2ascii

it's a exe in jdk\bin path. it mean: native code to ascii
cmd format:
native2ascii -encoding [charset] inputfile outputfile
sample:
native2ascii -encoding utf-8 a.properties b.properties
compile a file(saved as utf-8 before) to b(will save as ansi)

native2ascii -encoding gbk a.properties b.properties
compile a file(saved as ansi but chinese code by gbk before) to b(will save as ansi)

it's word as below:
convert all especial character of inputfile into of unicode(\uxxxx) outputfile,
and save outputfile as ansi format, so we can use it without indicate any charset. property will auto get the especial character with unicode.


InputStream stream=new FileInputStream("demo.properties");
Property p=new Property;
p.load(stream)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值