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 好听的元旦简短祝福语