`
wangboak
  • 浏览: 20133 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java Properties 乱码问题解决

阅读更多
Properties 是java提供的解决存储在文件中的键值对的一个类,继承了HashTable,能够处理java的标准配置文件。

但是Properties 在从文件流中读取文本时,getProperty(key)是使用ISO8859-1来解码的,所以读取中文时会乱码,需要将读取出来的字符串从ISO8859-1再编码回去,用文本的本身编码格式再解码。
String s = new String(properties.getProterty(key).getBytes[]("ISO8859-1"),"UTF-8");
UTF-8是本身的编码格式。

注意:在Properties.load
FileInputStream isr = new FileInputStream(savePath);
		Properties props = new Properties();
		props.load(isr);



注意:isr 必须是文件字节流,不能是字符流,否则还是会乱码。(或者有解决办法,但是我还没找到!)
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(savePath), "UTF-8");
props.store(osw,"This is the System Config file,Please don't delete or modify it!");

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics