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; } }