/**
* 在当前类路径或类的根路径下查找文件
* @param fileName
* @return Properties prop
*/
public static Properties load(String fileName) {
Properties prop = new Properties();
InputStream is = ZhPropertiesUtil.class.getResourceAsStream(fileName);
try {
if(is==null){//根路径下去试
String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
is =new FileInputStream(path+"/"+fileName);
}
prop.load(is);
} catch (Exception e) {
//throw new RuntimeException("导入属性文件出错!", e);
System.out.println("导入属性文件出错!/n"+e.toString());
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return prop;
}
JAVA读配置文件--在类的根路径及当前类的路径下
本文介绍了一种在Java中用于加载属性文件的方法。该方法首先尝试从类路径中加载文件,如果未找到,则会尝试从类的根路径加载。通过使用InputStream读取文件并将其加载到Properties对象中,确保了属性文件可以被正确读取和使用。

被折叠的 条评论
为什么被折叠?



