java.util.Properties和java.util.ResourceBundle类区别

ResourceBundle

一般来说,ResourceBundle类通常是用于针对不同的语言来使用的;

如果程序中的属性文件只是一些配置,并不是针对多国语言,那么使用Properties。

ResourceBundle,其作用是国际化,他的国际化文件也是.properties文件,只不过不用Properties类解析,而是用的ResourceBundle类来解析而已。

这个被ResourceBundle解析的.properties文件必须有命名规范: basename_语言代码_国家码.properties

如果是默认的,直接写为:basename.properties

系统默认的资源文件:只有basename,没有提供语言和地区。

ResourceBundle.getBundle():

    /**
     * return a resource bundle for the given base name and the default locale: Locale.getDefault()
     */
    public static final ResourceBundle getBundle(String baseName);

    /**
     * @return a resource bundle for the given base name and locale
     */
    public static final ResourceBundle getBundle(String baseName,
						 Locale locale);

Locale

Locale 对象表示了特定的地理、政治和文化地区。需要 Locale 来执行其任务的操作称为语言环境敏感的操作,它使用 Locale 为用户量身定制信息。例如,显示一个数值就是语言环境敏感的操作,应该根据用户的国家、地区或文化的风俗/传统来格式化该数值。

使用此类中的构造方法来创建 Locale:
 Locale(String language)
 Locale(String language, String country)
 Locale(String language, String country, String variant)

Java读取Properties文件的方法

1. 使用java.util.Properties类的load()方法

1.1 使用InputSteam系列方法

InputStream in = new BufferedInputStream(new FileInputStream(name)); 
Properties p = new Properties(); 
p.load(in);

1.2 使用class变量的getResourceAsStream()方法

InputStream in = Properties.class.getResourceAsStream(name);
Properties p = new Properties(); 
p.load(in);

2. 使用java.util.ResourceBundle类的getBundle()方法

ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值