码迷,mamicode.com
首页 > 编程语言 > 详细

一个读取propeties配置文件的工具类,线程安全的

时间:2016-10-11 10:45:38      阅读:365      评论:0      收藏:0      [点我收藏+]

标签:

public class ConfigUtil {

private static Map<String,Properties> map = new HashMap<String,Properties>();

/**
* 根据Properties文件名称获取Properties对象
* @param name
* @return Properties
* @throws IOException
*/
private synchronized static Properties createProperties(String name) throws IOException{
Properties p = map.get(name);
if(p == null){
p = new Properties();
p.load(ConfigUtil.class.getResourceAsStream(name));
map.put(name, p);
}
return p;
}
/**
* 根据Properties文件名和其中的key获取value
* @param proName, key
* @return String value
* @throws IOException
*/
public static String getValue(String proName,String key) throws IOException{
return createProperties(proName).getProperty(key);
}
/**
* 根据mqttconfig.properties文件的key获取value
* @param String key
* @return String value
* @throws IOException
*/
public static String getValue(String mqttKey) throws IOException{
return createMQTTProperties().getProperty(mqttKey);
}
/**
* 专门获取mqttconfig.properties的Properties
* @param
* @return Properties p
* @throws IOException
*/
public static Properties createMQTTProperties() throws IOException{
Properties p = map.get("mqttconfig.properties");
if(p == null){
p = new Properties();
p.load(ConfigUtil.class.getResourceAsStream("/mqttconfig.properties"));
map.put("mqttconfig.properties", p);
}
return p;
}
public static void main(String[] args) throws IOException{
System.out.println(getValue("/mqttconfig.properties","port"));
}
}

一个读取propeties配置文件的工具类,线程安全的

标签:

原文地址:http://www.cnblogs.com/zhaoblog/p/5948098.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!