java读取Properties文件

来源:互联网 发布:安卓网络机顶盒论坛 编辑:程序博客网 时间:2024/05/29 13:48

1.this.class.getClassLoader()怎么理解?
this.class是获得这个类相对于Class类的对象。getClassLoader()是获得这个类对象的加载器。只有Class类才有getClassLoader()方法;因为.class.getClassLoader()是为了获得一个类加载器,用来加载classpath下的.class文件而已。

2.代码
/**
* 获取配置文件中的配置项
* @FILE_NAME 配置文件的路径
* @name 配置文件中的配置项的名称
*/
public static String getConfig(String name){
Properties pro = new Properties();
InputStream in = null;
try {
in=PropertiesUtils.class.getClassLoader().getResourceAsStream(FILE_NAME);
pro.load(in);
return pro.getProperty(name);
} catch (IOException e) {
e.printStackTrace();
}finally{
if(in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}

3.代码截图
这里写图片描述