java src 文件路径_java中获得src路径下文件的常用方法
在代碼中一般讀取src下的配置文件
讀取src路徑下的log4j.properties和message.properties
讀取message.properties文件并將properties中的鍵值對轉為map
PropertiesServlet.class.getClassLoader().getResourceAsStream("/message.properties");返回值是一個InputStream
/**
*?根據java標準properties文件讀取信息,并賦值為一個?HashMap
*?@param?path
*?@param?map
*?@return
*?@throws?Exception
*/
public?final??Map?fileToMap(Map?map)?throws?Exception{
if(map?==?null){
map?=?new?HashMap();
}
InputStream?isr?=?null;
Reader?r?=?null;
try?{
isr?=?PropertiesServlet.class.getClassLoader().getResourceAsStream("/message.properties");
r?=?new?InputStreamReader(isr,?"UTF-8");
Properties?props?=?new?Properties();//使用..Properties
props.load(r);
Set>?entrySet?=?props.entrySet();
for?(Entry?entry?:?entrySet)?{
if?(!entry.getKey().toString().startsWith("#"))?{
map.put(((String)?entry.getKey()).trim(),?((String)?entry
.getValue()).trim());
}
}
return?map;
}?finally?{
if?(r?!=?null)?{
try?{
r.close();
}?catch?(IOException?e)?{
HLogger.error(e);
}
}
if?(isr?!=?null)?{
try?{
isr.close();
}?catch?(Exception?e2)?{
HLogger.error(e2);
}
}
}
}
LogInit.class.getClassLoader().getResource("//").getPath();返回的是一個String類型的路徑
public?void?init()?throws?ServletException?{
//?TODO?Auto-generated?method?stub
super.init();
PropertyConfigurator.configure(LogInit.class.getClassLoader().getResource("//").getPath()+"//log4j-hrp.properties");
}
總結
以上是生活随笔為你收集整理的java src 文件路径_java中获得src路径下文件的常用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 编写程序python输入任意大的自然数、
- 下一篇: java 连接池实例_功能完善的Java