java加载配置文件properties中的数据

时间:2022-04-08 11:59:17

在类路径下存在conf/mail.properties文件

内容:

host=test

加载该文件:

工具类:

public class PropertiesUtils {    private static PropertiesUtils proper = new PropertiesUtils();

private PropertiesUtils() {
}

public static PropertiesUtils getInstance() {
return proper;
}

public Map<String, String> load(String fileName) {
HashMap map = new HashMap();
if(StringUtils.isEmpty(fileName)) {
return map;
} else {
ResourceBundle bundle = ResourceBundle.getBundle(fileName);
Iterator i$ = bundle.keySet().iterator();

while(i$.hasNext()) {
String key = (String)i$.next();
map.put(key, bundle.getObject(key).toString());
}

return map;
}
}
}

读取配置:

public class SystemContent {
private static Map<String,String> mailMap;

static{
mailMap=PropertiesUtils.getInstance().load("conf/mail");
}

public static final String host=mailMap.get("host");
}