Use ResourceBundle in Java to access the resource file (properties file)

Source: Internet
Author: User

Constants are often used in the program. If they are directly written in the program, they must be re-compiled when these strings are changed, such as the database driver, URL, user name, and password information when writing the database application, you can use resource files (Properties files). Java provides the ResourceBundle class to facilitate access to attribute files. This article describes how to write the properties file, how to access it, and how to internationalize it.

1. Compiling the properties File

The file suffix is. properties.

The format of each row in the file is: key = value

For example:

Database. driver = com. mysql. jdbc. Drvier

Database. url = jdbc: mysql: // localhost: 3306: test

Database. user = root

Database. pass = root

If you use annotations, you can use #, for example:

# If other databases are used, modify the information.

2. Use ResourceBundl to parse resource files

In two steps: load the resource file and obtain a specific information.

2.1 load resource files

Use the getBundle method. The parameter is of the resource file.

ResourceBundle resource = ResourceBundle. getBundle ("messages ");

The parameter is the name of the resource file. Do not add a suffix.

2.2 obtain information in the resource file

Use the getString method. For example, to obtain the Driver written above, you can use the following code:

String driverName = resource. getString ("database. driver ");

3. Instance

3.1 resource files

File Name: database. properties

File Content:

Database. driver = com. mysql. jdbc. Drvier
Database. url = jdbc: mysql: // localhost: 3306: test
Database. user = root
Database. pass = root

3.2 read the Java file of the resource file

Package ch6;

Import java. util. ResourceBundle;

Public class ResourceBundleTest {

Public static void main (String [] args ){
ResourceBundle resource = ResourceBundle. getBundle ("ch6.database ");
String driverName = resource. getString ("database. driver ");
String url = resource. getString ("database. url ");
String user = resource. getString ("database. user ");
String pass = resource. getString ("database. pass ");

System. out. println ("Driver:" + driverName );
System. out. println ("URL:" + driverName );
System. out. println ("username:" + driverName );
System. out. println ("Password:" + driverName );
}
}

3.3 execution results

Driver: com. mysql. jdbc. Drvier
URL: com. mysql. jdbc. Drvier
Username: com. mysql. jdbc. Drvier
Password: com. mysql. jdbc. Drvier

4. Use Locale to set countries and regions to support internationalization

Some applications need to support multiple languages at the same time. At this time, it will be very laborious and difficult to maintain a set of applications for each language, A good way is to store unchanged logic content in the application instead of language-free information in the resource file, when necessary, load variable content from the resource file. Internationalization is to solve this problem. Write information in different languages in different files. Call the corresponding file as needed.

4.1 compile resource files that support multiple languages.

Default file: message. properties

Login. user = Username

Login. pass = Password

Login. submit = Submit

Simplified Chinese: message_zh_CN.properties

Login. user = user Name

Login. pass = Password

Login. submit = submit

Note: Chinese resource files must be processed using native2ascii and converted to Unicode encoding.

4.2 call different resource files

Use the getBundle method of ResourceBundle. The first parameter is the name of the resource file, and the second parameter is the country and region. For example:

ResourceBundle. getBundle ("ch6.message", Locale. SIMPLIFIED_CHINESE );

Note: Locale defines many constants that represent different languages. You can also directly specify the region and language.

Locale bLocale = new Locale ("en", "US ");
Locale cLocale = new Locale ("en", "GB ");


The first parameter is the language code, and the second parameter is the country code.

The language is as follows:

Language Code Description
De German
En English
Fr French
Ja Japan
Jw Javanese
Ko Korean
Zh Chinese


The Country Code is as follows:

Country Code Description
CN China
DE Germany
FR France
IN India
US United States


End!

Li xucheng CSDN Blog: http://blog.csdn.net/javaeeteacher
CSDN student base camp: http://student.csdn.net/space.php? Uid = 1, 124362
If you like my article, add me as a friend: http://student.csdn.net/invite.php? U= 124362 & c = 7be8ba2b6f3b6cc5

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.