zak=10.200.120.1
private static String zak = "";
static{
Map resultMap = PropertiesUtils.getProperties("wtcservice.properties");
if("".equals(zak)){
zak = (String)resultMap.get("zak");
}
}
String sleep_time_str = PropertiesUtils.getValue("sleep_time");//email_config.properties文件中的内容
- PropertiesUtils工具类
- MyEclipsez中配置文件路径在工程路径下的config文件夹下。
- 服务器上配置文件位于classes文件夹下
package nc.util.foundation;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
public class PropertiesUtils{
//此处可以加入多个文件
public static String[] filepath = {"/email_config.properties","/task.properties"};
public static Map map = new HashMap();
public static Map getProperties(String filename){
Properties properties = new Properties();
Map temmap = new HashMap();
InputStream is = null;
try{
is = PropertiesUtils.class.getResource(filename).openStream();
properties.load(is);
tempmap.putAll(properties);
}catch(Exception e){
throw new RuntimeException(e);
}finally{
properties.clear();
if(is != null){
try{
is.close();
}catch(IOException e){
thorw new RuntimeException(e);
}
}
}
return tempmap;
}
//具体取值方法
public synhronized static String getValue(String key){
if(map.size() == 0){
for(int i = 0; i < filepath.length; i++){
Properties properties = new Properties();
InputStream is = null;
try{
is = PropertiesUtils.class.getResource(filepath[i]).openStream();
properties.load(is);
//校验key是否存在重复
for(Iterator iter = map.keySet().iterator(); iter.hasNext();){
Object element = (Object) iter.next();
if(properties.containsKey(element)){
throw new RuntimeException("文件中已经存在key:"+ element + "");
}
}
map.putAll(properties);
}catch(Exception e){
throw new RuntimeException(e);
}finally{
properties.clear();
if(is != null){
is.close();
}catch(IOException e){
throw new RuntimeException(e);
}
}
}
}
}
return (String)map.get(key);
}
评论前必须登录!
注册