解析properties资源文件

[/code][code="java"]import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;


/** *//**
* 功能:解析资源文件
*
*/
public class PropertyUtil {

private Properties property=new Properties();
private String filepath="";

/** *//**
* 解析资源文件的类的构造函数
* @param path 文件索在的包,如:com.cn.myproperty.dbo.properties
*/
public PropertyUtil(String path){
try {
filepath=this.getClass().getResource("/").getPath();
filepath=filepath+path;


property.load(new FileInputStream(filepath));
} catch (Exception e) {
e.printStackTrace();
}
}


/** *//**
* 返回资源文件的hashmap对象
*/
public HashMap getElements(){
HashMap map=new HashMap ();
Iterator it=this.property.keySet().iterator();
while(it.hasNext()){
String key = it.next().toString();
map.put(key, property.getProperty(key));
}
return map;
}


//返回资源文件里的值
public String getvalue(String key){
if(key!=null)
return this.property.getProperty(key);
else
return "";
}

//设置资源文件里的值
public boolean setvalue(String key,String value){
if(key!=null)
{
try{
this.property.setProperty(key,value);
this.property.store(new FileOutputStream(filepath), filepath);
return true;
}catch(Exception ex){
System.out.println("保存资源文件出错了"+this);
}
}
return false;
}


}


import java.util.HashMap;


public class ConfigManager {
private static HashMap map=null;
static{
if(map==null){
PropertyUtil propertyUtil = new PropertyUtil("property.properties");
map=propertyUtil.getElements();
}
}
/** *//**
* 得到SystemConfig资源文件中定义的值,如果未定义则返回空字符串,不是null
* @param key
* @return
*/
public static String getValue(String key){
try{
return map.get(key).toString();
}catch(Exception ex){
System.out.println(ConfigManager.class+">>> zi yuan wen jian mei you ding yi key suo dui ying de zhi");
return "";
}
}

}

页面使用:
ConfigManager.getValue(******)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值