您的位置:首页 > 其它

web项目 读取src下的配置文件

2011-06-26 23:26 495 查看
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐