您的位置:首页 > 运维架构

property配置文件读取工具类

2013-11-18 21:57 218 查看
//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配置文件进行读取的,在使用的过程中只能被实例化一次。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: