读书人

java Properties 乱码有关问题解决

发布时间: 2012-10-07 17:28:51 作者: rapoo

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

读书人网 >编程

热点推荐