web工程下获取properties文件

String propertiesPath = getClass().getClassLoader().getResource("").getFile();
propertiesPath = propertiesPath.substring(1, propertiesPath.length());
//windows下
InputStream in = new FileInputStream(new File(propertiesPath + "/resource.properties"));
//linux下
//InputStream in = new FileInputStream(new File("/opt/apache-tomcat-6.0.26/webapps/IMS/WEB-INF/classes/resource.properties"));
Properties prop = new Properties();
prop.load(in);
Enumeration e = prop.propertyNames();
while(e.hasMoreElements()){
  String propKey = (String)e.nextElement();
  String propValue = prop.getProperty(propKey);
  propertiesMap.put(propKey, propValue);
}
in.close();

猜你喜欢

转载自xiyuliuguang.iteye.com/blog/1942548