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配置文件进行读取的,在使用的过程中只能被实例化一次。
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配置文件进行读取的,在使用的过程中只能被实例化一次。
相关文章推荐
- java读取xml和property配置文件的方法
- 根据配置文件名读取配置文件的工具类方法
- Java读取Property配置文件
- 通过spring工厂读取property配置文件
- Java读取property配置文件
- VBScript的PDF转换工具.6.辅助函数:配置文件读取
- 工具(一):properties配置文件读取工具类
- java读取项目里配置文件properties的工具
- java读取配置文件property
- 经过修改的读取配置文件PROPERTIES的小工具,对象尽量创建一次
- 读取jar包内外配置文件,xml property文件的读取修改
- 使用spring工厂读取property配置文件示例代码
- (4-1)Unity3D/工具:6步学会在Unity中读取Excel配置文件
- 读取配置文件 properties类工具
- 读取配置文件工具demo
- spring配置:context:property-placeholder 读取配置文件信息 在配置文件中使用el表达式填充值
- 配置文件读取工具类
- 读取XML配置文件的工具类
- 使用Spring提供的工具读取配置文件的信息
- spring读取项目外部property配置文件