1. 读取配置文件的小方法:
public String getValueByKeyFromProperties(String key, String filePath) {
// filePath = "d:\\test\\test.properties";
String value = "";
try {
FileInputStream fileInputStream = new FileInputStream(filePath);
Properties properties = new Properties();
properties.load(fileInputStream);
value = properties.getProperty(key);
fileInputStream.close();
} catch (FileNotFoundException fnfe) {
System.out.println("File \"" + filePath + "\" not found.");
return value;
} catch (Exception e) {
System.out.println("read \"" + filePath + "\" error.");
return value;
}
return value;
}
2. 得到中文乱码解决方法(UTF-8为配置文件的编码,根据具体情况更改)
String value = getValueByKeyFromProperties("key", "D:\\test\\test.properties");
if (!"".equals(value)) {
value = new String(value.getBytes("ISO-8859-1"), "UTF-8");
}