package com.test;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.PropertyResourceBundle;
import java.util.Set;
import java.util.Map.Entry;
public class ReadPropertiesFile {
/**
* @param args
*/
public static void main(String[] args) {
Map<String, String> map = ReadPropertiesFile.config();
Set<Entry<String, String>> s = map.entrySet();
for (Entry<String, String> entry : s) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
private static Map<String, String> config() {
Map<String, String> map = new HashMap<String, String>();
// src下的dataConfig.properties配置文件
String fileName = "dataConfig";
// 加载配置文件
PropertyResourceBundle resourceBundle = (PropertyResourceBundle) PropertyResourceBundle
.getBundle(fileName);
// 分解配置文件下的条目至一个枚举
Enumeration<String> enu = resourceBundle.getKeys();
// 组装成java可以识别处理的键值对
while (enu.hasMoreElements()) {
String propertyName = enu.nextElement().toString();
map.put(propertyName, resourceBundle.getString(propertyName));
}
return map;
}
}