注意,.properties文件放在根目录即可。
String classpath = Thread.currentThread().getContextClassLoader().getResource("/").getPath(); //获取根目录
String fileName = classpath + "truths.properties"; //配置文件名称
Properties p = new Properties();
FileInputStream fis;
try {
fis = new FileInputStream(fileName);
p.load(fis); //初始化
return p.getProperty("server"); //key名称
} catch (Exception e) {
e.printStackTrace();
}
防止中文乱码以及防止%20 发生惨案.
String classpath = Thread.currentThread().getContextClassLoader().getResource("/").getPath();
classpath = URLDecoder.decode(classpath,"UTF-8 "); //liunx 环境下,防止" " 转化为%20 而路径报错
String fileName = classpath + "truths.properties";
Properties p = new Properties();
FileInputStream fis = new FileInputStream(fileName);
//防止中文乱码/.
BufferedReader bf = new BufferedReader(new InputStreamReader(fis));
p.load(bf);
p.getProperty("server");简单暴力. 直接使用.
http://blog.csdn.net/buster2014/article/details/42045225
http://www.lai18.com/content/2659688.html
本文介绍了几种在Java中读取.properties文件的方法,包括如何避免中文乱码及解决路径中的%20问题。通过使用FileInputStream结合BufferedReader可以有效加载含有中文的属性文件。

199

被折叠的 条评论
为什么被折叠?



