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 婚礼父母在家祝福语简短