private URL getURLOfClassPath() {
return this.getClass().getClassLoader().getResource("/");
}
private String getAbsolutePathOfClassPath() {
try {
URI uri = this.getURLOfClassPath().toURI();
File tmp = new File(uri);
return tmp.getAbsolutePath();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public JSONObject loadConfig(String ckType) {
String absolutePathOfClassPath = this.getAbsolutePathOfClassPath();
String pathStr = absolutePathOfClassPath.replace(File.separator, "/");
pathStr = pathStr.substring(0, pathStr.indexOf("WEB-INF/classes"));
File file = new File(pathStr + ckType + "Config.json");
String jsonStr = "";
try {
FileReader fr = new FileReader(file);
BufferedReader bfr = new BufferedReader(fr);
String tmp = null;
while ((tmp = bfr.readLine()) != null) {
jsonStr += tmp;
}
return (JSONObject) JSONSerializer.toJSON(jsonStr);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
J2EE 在WEB-INF classes下读取配置文件
最新推荐文章于 2022-09-12 00:49:37 发布