java读取properties中文乱码

java在读取properties文件时如果配置文件中有中文可能遇到乱码问题。

首先,配置文件配置为utf-8格式并保存。

方法一:

Properties cfgFile = new java.util.Properties();
Test test = new Test();
String dir = “/init.properties”;
try {
java.io.InputStreamReader cfgin = new InputStreamReader(test.getClass().getResourceAsStream(dir),”UTF-8″);
cfgFile.load(cfgin);
String orglist = cfgFile.getProperty(“cmssynorglist”);
System.out.println(orglist);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}

方法二:

使用myeclipse打开文件,在properties视图中输入中文,保存。此时再打开source视图会发现中文已经被编码,例如\u7535\u52A1\u5904。

This entry was posted in Computer, Java. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s