问题
在IDEA下读取properties的属性文件会可能会导致乱码。这个问题主要原因可能存在于几个方面:可能是项目的编码问题、文件的编码问题、读取中使用的输入流的编码问题,这需要逐一的进行确认及更改。
解决
1、确认项目及属性文件编码
打开对话框File –> Settings
2、读取文件的方式
注意其中的输入流的创建过程,需要指定编码类型
try{
//加载文件
String formatterFileName = Platform.CORE.getPlatformEnName() + StaticConsts.FORMATTER_FILE_EXT;
InputStreamReader inputStream = new InputStreamReader(ClassLoader.getSystemResourceAsStream(formatterFileName),"UTF-8");
Preconditions.checkNotNull(inputStream, "core log formatter file " + formatterFileName + " not exists");
properties = new Properties();
properties.load(inputStream);
}catch(Exception e){
log.error("load core properties formatter file error",e);
}