zoukankan      html  css  js  c++  java
  • ResourceBundle类读取properties文件

    1.Properties与ResourceBundle类都可以读取属性文件key/value的键值对

    2.ResourceBundle类主要用来解决国际化和本地化问题,国际化时properties文件命名规范:

      一般的命名规范是: 自定义名语言代码国别代码.properties,如果是默认的,直接写为:自定义名.properties。

      例如:

      res_en_US.properties 
      res_zh_CN.properties                                       res.properties

      系统会自动选择属性资源文件

    3.使用:

      ResourceBundle bundle = ResourceBundle.getBundle("res", new Locale("zh", "CN"));

      new Locale(“zh”, “CN”)提供本地化信息

      程序会首先在classpath下寻找res_zh_CN.properties文件,若res_zh_CN.properties文件不存在,则取找res_zh.properties,如还是不存在,继续寻找res.properties,若都找不到就抛出异常。

    • 获取ResourceBundle实例bundle 后可以通过下面的方法获得本地化值。
    • getObject(String key);
    • getString(String key);
    • getStringArray(String key);

     如:bundle = ResourceBundle.getBundle("res", Locale.US);

      bundle = ResourceBundle.getBundle("res", Locale.getDefault());

      value= bundle.getString("key");

    注意:ResourceBundle.getBundle("xxx", Locale.US);

      ResourceBundle类基于类读取属性文件:将属性文件当作类,意味着属性文件必须放在包中(或src根目录下),使用属性文件的全限定性类名而非路径指代属性文件。

      ResourceBundle bundle = ResourceBundle.getBundle("com.rong.res.application");此时读取的是com.rong.res包下的application.properties属性文件

  • 相关阅读:
    【C#】调度程序进程已挂起,但消息仍在处理中;
    【WCF】wcf不支持的返回类型
    power design教程
    C# 动态修改Config
    【C#】delegate(委托) 将方法作为参数在类class 之间传递
    【.NET】Nuget包,把自己的dll放在云端
    【C#】C# 队列,
    【Chrome】新建Chrome插件,新建,事件行为,本地存储
    【IIS】iis6.1下添加两个ftp站点,
    【Jquery】$.Deferred 对象
  • 原文地址:https://www.cnblogs.com/57rongjielong/p/8878096.html
Copyright © 2011-2022 走看看