properties配置文件

package util;

import java.io.FileInputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* 属性文件配置解析类
*
*/
public class ParseProperties {
private ParseProperties(){}
private static Map<String,String> maps = new HashMap<String,String>();//缓存

/**
* 初始化
*/
private static void init(){
FileInputStream input=null;
try{
maps.clear();
input = new FileInputStream(System.getProperty("user.dir") + "/config/parameter.properties"); 
Properties props = new Properties();
props.load(input);
Enumeration<Object> em = props.keys();
while(em.hasMoreElements()){
Object obj = em.nextElement();
if(obj instanceof String){
maps.put((String)obj, props.getProperty((String)obj));
}
}
}catch(Exception e){
Config.getLog().writeLog(3, "属性文件配置解析类失败: "+ e.getMessage());
}finally{
try {
if(input!=null){
input.close();
}
} catch (Exception e) {
Config.getLog().writeLog(3, "属性文件配置解析类失败: "+ e.getMessage());
}
}
}

/**
* 根据键取值
* @param key
* @return
*/
public static String getValue(String key){
if(maps.size() == 0){
init();
}
return maps.get(key);
}

}

  

posted @ 2017-12-15 14:37  杯子茶壶  阅读(54)  评论(0)    收藏  举报