Java语言对properties资源文件的处理 - ResourceBundle类的用

Java语言提供了ResourceBundle类来对properties类型的资源文件加以处理。


本文对ResourceBundle类做一个解说。

开始之前,我们先解释一下什么是properties类型的资源文件。

在Java语言中,使用一种以.properties为扩展名的文本文件作为资源文件,该类型的文件的内容格式为类似:

#注释语句
some_key=some_value

形式。以#开头的行作为注释行,ResourceBundle类处理时会加以忽略;其余的行可以以 key名=value值 的形式加以记述。

Java的ResourceBundle类可以对这种形式的文件加以处理。

ResourceBundle类的使用方法也非常简单。我们使用一个例子来说明。


我们假设有下面2个properties文件:
TestProperties.properties
<script src="http://www.lifevv.com/images/code/js/shCore.js" type="text/javascript"></script> <script> <!--// function _syboos_pmplat_setUpCode() { //dp.SyntaxHighlighter.ClipboardSwf = 'js/clipboard.swf'; dp.SyntaxHighlighter.HighlightAll('_syboos_pmplat_code'); } window.οnlοad=_syboos_pmplat_setUpCode; //--> </script> <script src="http://www.lifevv.com/images/code/js/shBrushJava.js" type="text/javascript"></script>
  1. #key=value  
  2. userIdLabel=User Id:   
  3. userNameLabel=User Name:  
#key=value
userIdLabel=User Id: 
userNameLabel=User Name:


TestProperties_zh_CN.properties
  1. #key=value  
  2. userIdLabel=用户ID:   
  3. userNameLabel=用户名:  
#key=value
userIdLabel=用户ID: 
userNameLabel=用户名:

大家可能注意到TestProperties_zh_CN.properties文件名中有一个 _zh_CN名 称,该名称其实是用于资源文件的本地化处理。什么是本地化呢?我们简单说明一下:我们在进行系统开发时,很多时候需要为不同地区的用户准备不同的界面,比 如,如果一个系统同时面向 英语圈 的用户以及面向中国的用户,我们就必须为系统准备2套界面(包括消息),一套为英语界面,一套为中文界面。当然,除了界面不同之外,系统的处理过程完全一 样。当然我们不可能为它们分别开发2套不同的系统,怎么办呢?这就需要用到资源的本地化处理。也就是说,根据用户所处的地区或语言的不同,分别准备不同的 资源文件,这样就可以为不同的用户准备不同的界面但使用的却是同一套系统逻辑。

我们上面的2个文件就是2套不同的资源。

我们是使用ResourceBundle类处理不同资源的代码:

TestProperties.java
  1. package com.test.properties;  
  2.   
  3. import java.util.Enumeration;  
  4. import java.util.Locale;  
  5. import java.util.ResourceBundle;  
  6.   
  7. public class TestProperties  {  
  8.           
  9.     public static void main(String []args) {  
  10.         String resourceFile = "com.test.properties.TestProperties";  
  11.         //创建一个默认的ResourceBundle对象  
  12.         //ResourceBundle会查找包com.test.properties下的TestProperties.properties的文件  
  13.         //com.test.properties是资源的包名,它跟普通java类的命名规则完全一样:  
  14.         //- 区分大小写  
  15.         //- 扩展名 .properties 省略。就像对于类可以省略掉 .class扩展名一样  
  16.         //- 资源文件必须位于指定包的路径之下(位于所指定的classpath中)  
  17.         //另外,对于非西欧字符(比如中日韩文等),需要使用native2ascii命令或类似工具将其转换成ascii码文件格式,否则会显示乱码。  
  18.         System.out.println("---Default Locale---");  
  19.         ResourceBundle resource = ResourceBundle.getBundle(resourceFile);  
  20.           
  21.         testResourceBundle(resource);  
  22.   
  23.         System.out.println("---Locale.SIMPLIFIED_CHINESE---");  
  24.           
  25.         //创建一个指定Locale(本地化)的ResourceBundle对象,这里指定为Locale.SIMPLIFIED_CHINESE  
  26.         //所以ResourceBundle会查找com.test.properties.TestProperties_zh_CN.properties的文件  
  27.         //  
  28.         //中文相关的Locale有:  
  29.         //Locale.SIMPLIFIED_CHINESE : zh_CN  
  30.         resource = ResourceBundle.getBundle(resourceFile, Locale.SIMPLIFIED_CHINESE);  
  31.         //Locale.CHINA  : zh_CN  
  32.         //Locale.CHINESE: zh  
  33.         testResourceBundle(resource);  
  34.           
  35.         //显示  
  36.         //  
  37.     }  
  38.       
  39.     private static void testResourceBundle(ResourceBundle resource) {  
  40.         //取得指定关键字的value值  
  41.         String userIdLabel = resource.getString("userIdLabel");  
  42.         System.out.println(userIdLabel);  
  43.           
  44.         //取得所有key值  
  45.         Enumeration <String>enu = resource.getKeys();  
  46.           
  47.         System.out.println("keys:");  
  48.         while(enu.hasMoreElements()) {  
  49.             System.out.println(enu.nextElement());  
  50.         }  
  51.     }  
  52. }  
package com.test.properties;

import java.util.Enumeration;
import java.util.Locale;
import java.util.ResourceBundle;

public class TestProperties  {
        
    public static void main(String []args) {
        String resourceFile = "com.test.properties.TestProperties";
        //创建一个默认的ResourceBundle对象
        //ResourceBundle会查找包com.test.properties下的TestProperties.properties的文件
        //com.test.properties是资源的包名,它跟普通java类的命名规则完全一样:
        //- 区分大小写
        //- 扩展名 .properties 省略。就像对于类可以省略掉 .class扩展名一样
        //- 资源文件必须位于指定包的路径之下(位于所指定的classpath中)
        //另外,对于非西欧字符(比如中日韩文等),需要使用native2ascii命令或类似工具将其转换成ascii码文件格式,否则会显示乱码。
        System.out.println("---Default Locale---");
        ResourceBundle resource = ResourceBundle.getBundle(resourceFile);
        
        testResourceBundle(resource);

        System.out.println("---Locale.SIMPLIFIED_CHINESE---");
        
        //创建一个指定Locale(本地化)的ResourceBundle对象,这里指定为Locale.SIMPLIFIED_CHINESE
        //所以ResourceBundle会查找com.test.properties.TestProperties_zh_CN.properties的文件
        //
        //中文相关的Locale有:
        //Locale.SIMPLIFIED_CHINESE : zh_CN
        resource = ResourceBundle.getBundle(resourceFile, Locale.SIMPLIFIED_CHINESE);
        //Locale.CHINA  : zh_CN
        //Locale.CHINESE: zh
        testResourceBundle(resource);
        
        //显示
        //
    }
    
    private static void testResourceBundle(ResourceBundle resource) {
        //取得指定关键字的value值
        String userIdLabel = resource.getString("userIdLabel");
        System.out.println(userIdLabel);
        
        //取得所有key值
        Enumeration <String>enu = resource.getKeys();
        
        System.out.println("keys:");
        while(enu.hasMoreElements()) {
            System.out.println(enu.nextElement());
        }
    }
}


解说:
1,为了便于理解,我们把解说放在Java源代码中了,这里不再详述了。
2,对于中文资源文件TestProperties_zh_CN.properties,需要使用native2ascii 命令将其转换为ascii码。例如:

native2ascii -encoding UTF-8 c:/TestProperties_zh_CN.properties c:/java/com/test/properties/TestProperties_zh_CN.properties

至于native2ascii的详细用法这里不做详述了。

3,将上面3个文件都保存在 c:/java/com/test/properties/ 目录下。其中TestProperties_zh_CN.properties为经过native2ascii转换后的文件。

4,编译执行,将会在屏幕上显示:
c:/java/javac com.test.properties.TestProperties.java

c:/java/java com.test.properties.TestProperties
---Default Locale---
User Id:
keys:
userNameLabel
userIdLabel
---Locale.SIMPLIFIED_CHINESE---
用户ID:
keys:
userNameLabel
userIdLabel

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值