引言
最近在维护一个两年前的系统,需要 调整配置文件中的内容,在编辑的时候, 发现在服务器上中文都是unicode类型显示,所以根本不能维护,当我将unicode 转换为中文显示的时候,发现程序读取出来的是乱码。我的项目使用的编码也是utf-8,但是我用Properties读取中文的时候,打印出来的总是乱码。
后来网上查了一下,得到如下结论:Properties 默认是按ISO-8859-1读取的,所以如果你想让它按照你想的格式显示就需要转换一下。
程序代码如下:
public static Properties getProperties(String name) {
if (!name.contains(".")) {
name += ".properties";
}
Properties p = new Properties();
InputStream inputStream = null;
try {
inputStream = PropertiesUtils.class.getClassLoader().getResourceAsStream("properties/" + name);
p.load(inputStream);
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {