java读取properties文件,中文乱码处理

时间:2024-02-16 08:17:41

查看properties乱码的问题。通过修改eclipse配置可以解决:(eclipse打开properties配置文件中文显示字符的编码

java读取properties文件,中文乱码处理

Java读取properties文件中文乱码问题的解决  

无论系统的默认编码是什么,在读取properties文件时使用的iso8859-1编码。

解决方法是:

读取的字符用字符编码:

        InputStream resource = TestOut.class.getClassLoader().getResourceAsStream("db.properties");
        Properties properties2 = new Properties();
        properties2.load(resource);
        String property = properties2.getProperty("java");
        System.out.println("----" + new String(property.getBytes("iso-8859-1")));

properties文件:

java=java项目

 

以下是API说明部分: 

When saving properties to a stream or loading them from a stream, the ISO 8859-1 character encoding is used. For characters that cannot be directly represented in this encoding, Unicode escapes are used; however, only a single \'u\' character is allowed in an escape sequence. The native2ascii tool can be used to convert property files to and from other character encodings.

下面是源码:BufferedReader in = new BufferedReader(new InputStreamReader(inStream,"8859_1"));