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

  • 相关阅读:
    实验4 类的继承、派生和多态(1)
    期中
    实验3 类和对象
    设计、定义并实现Complex类
    android——fargment基础
    android——自定义listView
    android——HttpUrlConnection
    客户端与服务端小知识
    android——handler机制原理
    学习笔记——SQLite介绍
  • 原文地址:https://www.cnblogs.com/57rongjielong/p/8878096.html
Copyright © 2020-2023  润新知