//property配置文件属性获取工具类
public class PropertyReadUtil{
private static PropertyReadUtil instance;
//公用的构造方法
private PropertyReadUtil(){}
//单例Singleton
public static PropertyReadyUtil getInstance(){
if(instance == null){
instance = new PropertyReadUtil();
}
return instance;
}
public String getUrl(String key){
//获取文件路径
InputStrean input = getClass().getResourceAsStream("xxx.property");
Properties pro = new Properties();
try{
//加载property文件
pro.load(input);
//返回property文件指定标识
return (String)pro.get(key);
}catch(IOException e){
e.printStackTrance();
}
return null;
}
public static void main(String [] args){
PropertyReadUtil pru = new PropertyReadUtil.getInstance();
String myName = pru.getUrl("myName");
//输出的内容为test
System.out.println(myName);
}
}
2.property配置文件已xxx.properties为后缀命名。
myName=test
本例使用的是Singleton对property配置文件进行读取的,在使用的过程中只能被实例化一次。