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属性文件

  • 相关阅读:
    Ubuntu上VNC 配置
    Ubuntu远程桌面xrdp方法
    sudo 免密码
    Ubuntu 12.04 root默认密码? 如何使用root登录?
    DNS 和 IPv6 配置攻略
    计算机专业学习浅谈
    [图像]张正友论文翻译(2)
    [图像]张正友论文翻译(1)
    [图像]用Matlab在图像上画矩形框
    word如何修改尾注
  • 原文地址:https://www.cnblogs.com/57rongjielong/p/8878096.html
Copyright © 2011-2022 走看看