解析properties资源文件
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(******)