java resourcebundle properties_Java使用Properties类和ResourceBundle类读取properties文件

一.介绍:

项目中经常把一些常用的用户名和密码都填写到一个对应的配置文件中,这样每次修改密码或者用户名的时候就可以直接修改这个配置文件了,不用动源码.

这里讲两种方式读取properties文件的方法.一个是用HashTable下的Properties类一个是用国际化的ResourceBundle类.

二.第一种:Properties类读取properties配置文件

下面的代码是在一个web工程中运行的,具体解释看代码中的注释:

1 importjava.io.InputStream;2 importjava.util.Properties;3

4 importcom.chinadigitalvideo.filter.PropertiesUtil;5

6 public classPropertiesAction {7 //把properties中用到的常量又和全局相关声明为static和final类型的.

8 public static finalString CLASSDRIVER ;9 public static finalString URL ;10 public static finalString NAME ;11 public static finalString PASSWORD ;12

13 public static finalString RETRYTIMES ;14 public static finalString LOCKDEADLINE ;15 /*

16 用到Javase中继承于HashTable的Properties类,来读取properties配置文件获得键值对信息.17 如果是读取一个普通的txt文本文件使用File类来读取。18 properties文件中都是一些静态的常量,所以把这块读取的代码写在静态代码块中 不是写在普通的方法19 */

20 static{21 Properties props = newProperties();22 /* 方式一:

23 加载读取该配置文件需要使用Properties的load方法,当然load()方法中是读取文件的流24 props.load(new FileInputStream("src/db.properties"));25 但是此项目是个web项目,打包发布到服务器上,war包中压根没有src目录,所以不能用用这中路径来读取文件.26 应该用类的加载器来读取这个文件.怎么通过类加载器获得这个db.properties文件.27 */

28 ClassLoader classLoader = PropertiesAction.class.getClassLoader();29 InputStream is = classLoader.getResourceAsStream("db.properties");30 try{31 props.load(is);32 } catch(Exception e) {33 e.printStackTrace();34 }35 CLASSDRIVER = props.getProperty("driverClass");36 URL = props.getProperty("url");37 NAME = props.getProperty("name");38 PASSWORD = props.getProperty("password");39 /*

40 方式二: 需要PropertiesAction.java 文件和 要读取的login.properties配置文件在一个目录下 */

43 InputStream inputStream = PropertiesAction.class.getResourceAsStream("login.properties");44 try{45 props.load(inputStream);46 } catch(Exception e) {47 e.printStackTrace();48 }49 RETRYTIMES = props.getProperty("retrytimes");50 LOCKDEADLINE = props.getProperty("lockdeadline");51 }52 }

为说明ClassLoader作用的根目录附录相关截图:

图1:Debug代码,官产ClassLoader对象的信息  和 项目的源码编译路径做比较

819b309835e8291379e4852c07e00510.png

图2:打成war包之后的目录如下,WebContent是这个web项目的根目录,打成war包后,所有的代码资源都在此目录下, 源码编译后的class文件在上图设置的路径/WEB-INF/classes下

a57382cee6a481d0ed25a4f681133422.png

7678bc18cf1200473b47c6abcf4cdbd1.png

三.ResourceBundle类读取Properties方法(此处也是放在web项目中用的)

1.示例代码

importjava.util.ResourceBundle;public classResourceBundleAction {//用到的常量又和全局相关声明成static 和final类型的.

public static finalString CLASSDRIVER ;public static finalString URL ;public static finalString NAME ;public static finalString PASSWORD ;static{/*javase中的提供的一个国际化的类 ResourceBundle类.使用这个类可以轻松地本地化或翻译成不同的语言

其中getBundle(String baseName)方法的作用是

baseName - 资源包的基本名称,是一个完全限定类名

具有给定基本名称和默认语言环境的资源包*/CLASSDRIVER= ResourceBundle.getBundle("db").getString("driverClass");

URL= ResourceBundle.getBundle("db").getString("url");

NAME= ResourceBundle.getBundle("db").getString("name");

PASSWORD= ResourceBundle.getBundle("db").getString("password");

}

}

2.java.util.ResourceBundle使用详解

说的简单点,这个类的作用就是读取资源属性文件(properties),然后根据.properties文件的名称信息(本地化信息),匹配当前系统的国别语言信息(也可以程序指定),然后获取相应的properties文件的内容。

使用这个类,要注意的一点是,这个properties文件的名字是有规范的:一般的命名规范是: 自定义名_语言代码_国别代码.properties,

如果是默认的,直接写为:自定义名.properties

比如:

myres_en_US.properties

myres_zh_CN.properties

myres.properties

当在中文操作系统下,如果myres_zh_CN.properties、myres.properties两个文件都存在,则优先会使用myres_zh_CN.properties,当myres_zh_CN.properties不存在时候,会使用默认的myres.properties

没有提供语言和地区的资源文件是系统默认的资源文件。

资源文件都必须是ISO-8859-1编码,因此,对于所有非西方语系的处理,都必须先将之转换为Java Unicode Escape格式。转换方法是通过JDK自带的工具native2ascii.

定义三个资源文件,放到src的根目录下面(必须这样,或者你放到自己配置的calsspath下面)。

TestResourceBundle.java

1 importjava.util.Locale;2 importjava.util.ResourceBundle;3

4 public classTestResourceBundle {5 public static voidmain(String[] args) {6 Locale locale1 = new Locale("zh", "CN");7 ResourceBundle resb1 = ResourceBundle.getBundle("myres", locale1);8 System.out.println(resb1.getString("aaa"));9

10 ResourceBundle resb2 = ResourceBundle.getBundle("myres", Locale.getDefault());11 System.out.println(resb1.getString("aaa"));12

13 Locale locale3 = new Locale("en", "US");14 ResourceBundle resb3 = ResourceBundle.getBundle("myres", locale3);15 System.out.println(resb3.getString("aaa"));16 }17 }

在src根目录下有:

myres.properties

aaa=good

bbb=thanks

myres_en_US.properties

aaa=good

bbb=thanks

myres_zh_CN.properties

aaa=\u597d

bbb=\u591a\u8c22

运行结果:

good

3.认识Locale

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

使用此类中的构造方法来创建 Locale:

Locale(String language)

Locale(String language, String country)

Locale(String language, String country, String variant)

创建完 Locale 后,就可以查询有关其自身的信息。使用 getCountry 可获取 ISO 国家代码,使用 getLanguage 则获取 ISO 语言代码。可用使用 getDisplayCountry 来获取适合向用户显示的国家名。同样,可用使用 getDisplayLanguage 来获取适合向用户显示的语言名。有趣的是,getDisplayXXX 方法本身是语言环境敏感的,它有两个版本:一个使用默认的语言环境作为参数,另一个则使用指定的语言环境作为参数。

1 packagecn.itcast.resoucebundle;2

3 importjava.text.MessageFormat;4 importjava.util.MissingResourceException;5 importjava.util.ResourceBundle;6

7 public classI18nMessages {8 public static final String KEY_NOT_FOUND_PREFIX = "!!!"; //$NON-NLS-1$

9

10 public static final String KEY_NOT_FOUND_SUFFIX = "!!!"; //$NON-NLS-1$

11

12 private static final String BUNDLE_NAME = "messages"; //$NON-NLS-1$

13 private static final String BUNDLE_NAME2 = "cn.itcast.interfaceAbstract.messages"; //$NON-NLS-1$

14

15 private static final String PLUGIN_ID = "cn.itcast.resourcebundle"; //$NON-NLS-1$

16

17 private static ResourceBundle resourceBundle =ResourceBundle.getBundle(BUNDLE_NAME2);18

19 public staticString getString(String key, String pluginId, ResourceBundle resourceBundle) {20 if (resourceBundle == null) {21 return KEY_NOT_FOUND_PREFIX + key +KEY_NOT_FOUND_SUFFIX;22 }23 try{24 returnresourceBundle.getString(key);25 } catch(MissingResourceException e) {26 return KEY_NOT_FOUND_PREFIX + key +KEY_NOT_FOUND_SUFFIX;27 }28 }29

30 public staticString getString(String key, String pluginId, ResourceBundle resourceBundle, Object... args){31 returnMessageFormat.format(getString(key, pluginId, resourceBundle),args);32 }33

34 public staticString getString(String key, Object... args){35 returngetString(key, PLUGIN_ID, resourceBundle, args);36 }37 public static voidmain(String[] args) {38 String test = "kxh";39 String test2 = "Yes,I am";40 System.out.println(I18nMessages.getString("name",test,test2));//这个方法设置的可以跟多个参数.41 }42 }

6d4d926d623ad33458e139644f9e2a5f.png

输出:

ResourceBundle.getBundle(BUNDLE_NAME2);的时候

Are you kxh?Yes,I am

This is the second one

ResourceBundle.getBundle(BUNDLE_NAME);的时候

Are you kxh?Yes,I am

This is the first one

对应的messages.properties中的参数设置

c11d1e870c35b2ca64bcd553aa7a2a27.png

f4c1b4b36ad65d31e3d8c0ae9044ed80.png

上面用到了MessageFormat.format()方法

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值