ResourceBundle读取属性文件

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

}

 

转载于:https://my.oschina.net/okqq/blog/723922

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值