Processing of properties resource files in Java-Use of the resourcebundle class

Source: Internet
Author: User
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?

  1. # Key = Value
  2. Useridlabel = user ID:
  3. Usernamelabel = User Name:
#key=valueuserIdLabel=User Id: userNameLabel=User Name:

Testproperties_zh_cn.propertiesview plainprint?

  1. # Key = Value
  2. Useridlabel = user ID:
  3. 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?

  1. Package com. Test. properties;
  2. Import java. util. enumeration;
  3. Import java. util. locale;
  4. Import java. util. resourcebundle;
  5. Public class testproperties {
  6. Public static void main (string [] ARGs ){
  7. String resourcefile = "com. Test. properties. testproperties ";
  8. // Create a default resourcebundle object
  9. // Resourcebundle searches for the testproperties. properties file under the package com. Test. properties.
  10. // Com. Test. properties is the package name of the resource. It is identical with the naming rules of common Java classes:
  11. //-Case sensitive
  12. //-The extension. properties is omitted. Just like the. Class extension can be omitted for a class.
  13. //-The resource file must be located under the path of the specified package (in the specified classpath)
  14. // 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.
  15. System. Out. println ("--- default locale ---");
  16. Resourcebundle resource = resourcebundle. getbundle (resourcefile );
  17. Testresourcebundle (Resource );
  18. System. Out. println ("--- locale. simplified_chinese ---");
  19. // Create a resourcebundle object that specifies locale (localization), which is specified as locale. simplified_chinese
  20. // So resourcebundle will find the file com. Test. properties. testproperties_zh_cn.properties.
  21. //
  22. // Locale related to Chinese characters:
  23. // Locale. simplified_chinese: zh_cn
  24. Resource = resourcebundle. getbundle (resourcefile, locale. simplified_chinese );
  25. // Locale. China: zh_cn
  26. // Locale. Chinese: ZH
  27. Testresourcebundle (Resource );
  28. // Display
  29. //
  30. }
  31. Private Static void testresourcebundle (resourcebundle Resource ){
  32. // Obtain the value of the specified keyword
  33. String useridlabel = resource. getstring ("useridlabel ");
  34. System. Out. println (useridlabel );
  35. // Obtain all key values
  36. Enumeration <string> enu = resource. getkeys ();
  37. System. Out. println ("keys :");
  38. While (enu. hasmoreelements ()){
  39. System. Out. println (enu. nextelement ());
  40. }
  41. }
  42. }
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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.