在很多业务中要取得WEB-INF下的文件.
网上的有很多人问过这种情况怎么取文件,有很多人建议把文件放在src下,个人感觉这哥们在放屁.
在此提供一种方法.
在struts中用到这个方式.
Properties properties = new Properties();
try {
// 得到当前路径
String url = this.getClass().getResource("").getPath()
.replaceAll("%20", " ");
// 得到WEB-INF路径
String path = url.substring(0, url.indexOf("WEB-INF"))
+ "WEB-INF/sysconfig.properties";
// 读取prop文件
properties.load(new FileInputStream(path));
} catch (IOException e) {
// e.printStackTrace();
}
只要路径找到了,任何文件不在话下.