`

note about property & charset

    博客分类:
  • java
 
阅读更多
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)


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics