日期:2014-05-18  浏览次数:20647 次

java web工程中读取properties文件,路径一直不知道怎么写
properties文件放在WEB-INF\config\uas-system.properties

我的代码是:

 //根据key读取value
public static String readValue(String key) {
Properties props = new Properties();
try {
InputStream in = new BufferedInputStream(new FileInputStream("/uas-operation.properties"));
props.load(in);
String value = props.getProperty(key);
System.out.println(key + value);
return value;
} catch (Exception e) {
e.printStackTrace();
return "";
}

试了很多路径都不对,请问这个路径问题怎么解决?谢谢

------解决方案--------------------
为什么不直接放在WEB-INF\classes下面,如果直接放在WEB-INF\classes下面就很容易了。
直接用resourcebundle或者Class.getresourceasstream都能够很容易获取到。

如果放在WEB-INF的config目录,我估计得先获取工程的目录,然后再根据工程目录的相对目录找到文件。
------解决方案--------------------
Class.getResourceAsStream(String path) 
path 不以’/'开头时默认是从此类所在的包下取资源,以’/'开头则是从ClassPath根下获取。