1、java.util.ResourceBundle类作用
这个类的作用就是读取资源属性文件(properties),然后根据.properties文件的名称信息(本地化信息),匹配当前系统的国别语言信息(也可以程序指定),然后获取相应的properties文件的内容。
使用这个类,要注意的一点是,这个properties文件的名字是有规范的:一般的命名规范是: 自定义名_语言代码_国别代码.properties,
如果是默认的,直接写为:自定义名.properties
比如:
config_en_US.properties
config_zh_CN.properties
config.properties
当在中文操作系统下,如果config_zh_CN.properties、config.properties两个文件都存在,则优先会使用config_zh_CN.properties,当config_zh_CN.properties不存在时候,会使用默认的config.properties。
没有提供语言和地区的资源文件是系统默认的资源文件。
2、对语言支持设置
Locale locale = new Locale("en", "US");
ResourceBundle prop = ResourceBundle.getBundle("config", locale);
3、源码 工具类
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
/**
* Config 读取系统的配置文件
*/
public class ConfigUtil {
private static Map<String, ResourceBundle> properties = new HashMap<String, ResourceBundle>();
private static ResourceBundle prop;
public static ResourceBundle load(String file){
prop = properties.get(file);
if(prop==null){
prop = ResourceBundle.getBundle(file);
properties.put(file, prop);
}
return prop;
}
public static int getInt(String key) {
return Integer.parseInt(prop.getString(key));
}
public static String getString(String key) {
return prop.getString(key);
}
public static double getDouble(String key) {
return Double.parseDouble(prop.getString(key));
}
public static ResourceBundle getProperties() {
return prop;
}
}