Java语言对properties资源文件的处理

开发 后端
Java语言提供了ResourceBundle类来对properties类型的资源文件加以处理。 本文对ResourceBundle类做一个解说。

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

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

#注释语句
some_key=some_value

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

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

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

我们假设有下面2个properties文件:

TestProperties.properties  
 
view plainprint?  
#key=value     
userIdLabel=User Id:      
userNameLabel=User Name:     
#key=value 
userIdLabel=User Id:   
userNameLabel=User Name:  
 
TestProperties_zh_CN.properties  
 
view plainprint?  
#key=value     
userIdLabel=用户ID:      
userNameLabel=用户名:     
#key=value 
userIdLabel=用户ID:   
userNameLabel=用户名: 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

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

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

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

TestProperties.java  
 
view plainprint?  
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 enu = resource.getKeys();     
     
System.out.println("keys:");     
while(enu.hasMoreElements()) {     
    System.out.println(enu.nextElement());     
}     
    }     
}     
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 enu = resource.getKeys();  
 
System.out.println("keys:");  
while(enu.hasMoreElements()) {  
    System.out.println(enu.nextElement());  
}  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.

解说:

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

【编辑推荐】

  1. Java连接MySQL中文乱码处理
  2. 在Java应用程序中使用Jfreechart配置
  3. Java虚拟机内部构成浅析
  4. 浅谈Java线程的生命周期
  5. 关于Java继承的一些复习
责任编辑:彭凡 来源: CSDN
相关推荐

2013-06-17 17:02:15

Windows PhoWP开发资源文件

2012-02-03 09:55:06

Delphi

2011-06-20 14:18:27

Qt 资源 文件

2009-08-21 16:13:27

C#读取资源文件

2011-08-15 14:47:28

Cocoa嵌入资源文件

2013-07-30 12:30:57

Windows PhoWindows Pho

2009-06-02 14:21:04

eclipse资源文件eclipse中文

2009-08-28 16:08:28

C#利用资源文件

2009-12-28 11:23:38

WPF资源文件

2020-08-25 07:16:20

Python资源文件文件

2009-08-12 16:44:13

.NET文件类型

2013-05-15 10:27:05

R语言

2010-08-24 16:07:37

C语言

2010-01-04 16:23:42

Silverlight

2009-10-28 11:27:34

VB.NET资源文件

2009-06-05 09:40:59

2010-01-15 18:50:25

VB.NET资源文件

2013-05-27 10:58:10

propertiesJava语言

2009-06-04 09:26:51

struts 源码struts 资源文件

2011-05-13 15:46:49

C模块化
点赞
收藏

51CTO技术栈公众号