properties文件放在resourses文件下
name=123
age=1
address=南京
phone=123456
url=http://www.baidu.com
static Properties prop = null ;
public static void loadProperty(){
//3种方法加载resourses目录下的spring.properties文件
// InputStream is = this.getClass().getClassLoader().getResourceAsStream("spring.properties");
InputStream is1 = PropertiesUtil.class.getResourceAsStream("/spring.properties");
// InputStream is1 = PropertiesUtil.class.getClassLoader().getResourceAsStream("spring.properties");
try {
prop = new Properties();
prop.load(is1);
} catch (IOException e) {
e.printStackTrace();
}finally {
if (is1 != null) {
try {
is1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static Object getProperty(String key){
if(prop == null){
loadProperty();
}
return prop.get(key);
}
this代表当前对象,不能在static方法中使用