为什么不直接放在WEB-INF\classes下面,如果直接放在WEB-INF\classes下面就很容易了。
直接用resourcebundle或者Class.getresourceasstream都能够很容易获取到。如果放在WEB-INF的config目录,我估计得先获取工程的目录,然后再根据工程目录的相对目录找到文件。
直接用resourcebundle或者Class.getresourceasstream都能够很容易获取到。如果放在WEB-INF的config目录,我估计得先获取工程的目录,然后再根据工程目录的相对目录找到文件。
path 不以’/'开头时默认是从此类所在的包下取资源,以’/'开头则是从ClassPath根下获取。
WEB-INF\config\uas-system.properties
我已经把properties文件放入classes文件夹下了,用以下代码可以:public static String readValue(String key) {
Properties props = new Properties();
try {
InputStream in=UasTagFunction.class.getResourceAsStream("/uas-operation.properties");
props.load(in);
String value = props.getProperty(key);
return value;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
Properties props = new Properties();
FileInputStream fis=null
try {
fis=new FileInputStream(new File(UasTagFunction.class.getResource("/").getPath()+"config\uas-system.properties"));
props.load(fis);
String value = props.getProperty(key);
return value;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
Properties props = new Properties();
FileInputStream fis=null
try {
fis=new FileInputStream(new File(UasTagFunction.class.getResource("/").getPath()+"config/uas-system.properties"));
props.load(fis);
String value = props.getProperty(key);
return value;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}