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