The Java language provides the resourcebundle class to process resource files of the properties type.
This article provides an explanation of the resourcebundle class.
Before starting, Let's explain what resources files of the properties type are.
In Java, a text file with the. properties extension is used as a resource file. The content format of this type of file is similar:
# Comment statement
Some_key = some_value
Format. A row starting with # is used as a comment row, which is ignored when processed by the resourcebundle class. Other rows can be described in the form of key = value.
Java's resourcebundle class can process such files.
The use of the resourcebundle class is also very simple. We use an example to illustrate.
Let's assume there are two properties files:
Testproperties. propertiesview plainprint?
- # Key = Value
- Useridlabel = user ID:
- Usernamelabel = User Name:
#key=valueuserIdLabel=User Id: userNameLabel=User Name:
Testproperties_zh_cn.propertiesview plainprint?
- # Key = Value
- Useridlabel = user ID:
- Usernamelabel = User Name:
# Key = valueuseridlabel = user ID: usernamelabel = User Name:
You may notice that there is a file name in the testproperties_zh_cn.properties file._ Zh_cnName
This name is actually used for localization of resource files. What is localization? In system development, we usually need to prepare different interfaces for users in different regions.
For example, if a system is oriented to English circles at the same time
For Chinese users, we must prepare two sets of interfaces (including messages) for the system, one for the English interface and the other for the Chinese interface. Of course, apart from different interfaces, the process of the system is completely one.
Sample. Of course, we cannot develop two different systems for them. What should we do? This requires localization of resources. That is to say, prepare different
Resource file. In this way, you can prepare different interfaces for different users, but use the same system logic.
The two files above are two sets of different resources.
We use the resourcebundle class to Process Code for different resources:
Testproperties. javaview 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 ";
- // Create a default resourcebundle object
- // Resourcebundle searches for the testproperties. properties file under the package com. Test. properties.
- // Com. Test. properties is the package name of the resource. It is identical with the naming rules of common Java classes:
- //-Case sensitive
- //-The extension. properties is omitted. Just like the. Class extension can be omitted for a class.
- //-The resource file must be located under the path of the specified package (in the specified classpath)
- // In addition, for non-Western European characters (such as Chinese and Japanese Korean), you need to use the native2ascii command or similar tools to convert it to the ASCII code file format. Otherwise, garbled characters are displayed.
- System. Out. println ("--- default locale ---");
- Resourcebundle resource = resourcebundle. getbundle (resourcefile );
- Testresourcebundle (Resource );
- System. Out. println ("--- locale. simplified_chinese ---");
- // Create a resourcebundle object that specifies locale (localization), which is specified as locale. simplified_chinese
- // So resourcebundle will find the file com. Test. properties. testproperties_zh_cn.properties.
- //
- // Locale related to Chinese characters:
- // Locale. simplified_chinese: zh_cn
- Resource = resourcebundle. getbundle (resourcefile, locale. simplified_chinese );
- // Locale. China: zh_cn
- // Locale. Chinese: ZH
- Testresourcebundle (Resource );
- // Display
- //
- }
- Private Static void testresourcebundle (resourcebundle Resource ){
- // Obtain the value of the specified keyword
- String useridlabel = resource. getstring ("useridlabel ");
- System. Out. println (useridlabel );
- // Obtain all key values
- Enumeration <string> 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 "; // create a default resourcebundle object // resourcebundle searches for the package COM. test. testproperties under properties. properties file // COM. test. properties is the package name of the resource, which is exactly the same as the naming rules of common Java classes: //-case sensitive //- The extension. properties is omitted. Just as the class can be omitted. same class extension //-The resource file must be located under the path of the specified package (in the specified classpath) // In addition, for non-Western European characters (such as Chinese and Japanese Korean ), you need to use the native2ascii command or similar tools to convert it to the ASCII code file format. Otherwise, garbled characters are displayed. System. out. println ("--- default locale ---"); resourcebundle resource = resourcebundle. getbundle (resourcefile); testresourcebundle (Resource); system. out. println ("--- locale. simplified_chinese --- "); // create a resourcebundle object that specifies locale (localization), which is specified as locale. simplified_chinese // so resourcebundle will look for com. test. properties. the file testproperties_zh_cn.properties /// locale related to Chinese characters: // locale. simplified_chinese: zh_cn resource = resourcebundle. getbundle (resourcefile, locale. simplified_chinese); // locale. china: zh_cn // locale. chinese: ZH testresourcebundle (Resource); // display //} Private Static void testresourcebundle (resourcebundle Resource) {// obtain the value of the specified keyword string useridlabel = resource. getstring ("useridlabel"); system. out. println (useridlabel); // obtain all key values enumeration <string> enu = resource. getkeys (); system. out. println ("keys:"); While (enu. hasmoreelements () {system. out. println (enu. nextelement ());}}}
Explanation:
1. For ease of understanding, we put the explanation in the Java source code, which will not be detailed here.
2. For the Chinese resource file testproperties_zh_cn.properties, use the native2ascii command to convert it to an ascii code. For example:
Native2ascii-encoding UTF-8 C:/testproperties_zh_cn.properties C:/Java/COM/test/properties/testproperties_zh_cn.properties
The detailed usage of native2ascii is not described here.
3. Save the above three files in the C:/Java/COM/test/properties/directory. Testproperties_zh_cn.properties is the file after native2ascii conversion.
4. Compile and execute the program. The following code is displayed on the screen: C:/Java/javac com. Test. properties. testproperties. java.
C:/Java com. Test. properties. testproperties
--- Default locale ---
User ID:
Keys:
Usernamelabel
Useridlabel
--- Locale. simplified_chinese ---
User ID:
Keys:
Usernamelabel
Useridlabel