Java如何将ResourceBundle转换为Properties?
package org.nhooo.example.util;
import java.util.*;
public class ResourceBundleToProperties {
public static void main(String[] args) {
//从类路径加载资源包Messages_en_GB.properties。
ResourceBundle resource = ResourceBundle.getBundle("Messages", Locale.UK);
//调用convertResourceBundleToProperties方法转换资源
//捆绑成一个Properties对象。
Properties properties = convertResourceBundleToProperties(resource);
//打印属性的全部内容。
Enumeration keys = properties.keys();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
String value = (String) properties.get(key);
System.out.println(key + " = " + value);
}
}
/**
* Convert ResourceBundle into a Properties object.
*
* @param resource a resource bundle to convert.
* @return Properties a properties version of the resource bundle.
*/
private static Properties convertResourceBundleToProperties(ResourceBundle resource) {
Properties properties = new Properties();
Enumeration<String> keys = resource.getKeys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
properties.put(key, resource.getString(key));
}
return properties;
}
}
热门推荐
10 姐姐女儿结婚祝福语简短
11 元旦的祝福语简短 公司
12 祝美术老师祝福语简短
13 国庆求婚祝福语简短精辟
14 信件告别祝福语大全简短
15 参加儿子大学祝福语简短
16 同事离别祝福语简短的
17 近代生日祝福语简短精辟
18 同事有趣祝福语大全简短