A property resource bundle is a collection of text elements in a properties
file format stored in a .properties file. The
PropertyResourceBundle class provides all code necessary
for you to access these resources in a locale-dependent manner.
Example 4-10 illustrates basic property file formatting. The
Properties class in the java. util package
handles reading and writing properties files. Both the keys to lookup
a value and the values returned are Java Strings. Example 4-
10 shows a simple properties file.
Example 4-10. A Properties File
# Sample properties file for demonstrating PropertyResourceBundle class
#
# Text that will appear in the title bar of our application
ApplicationTitle= Demonstrating the use of PropertyResourceBundle
#
# Text that will be displayed on OK buttons
OKButtonLabel= OK
#
# Text for display on a button to cancel current operation
CancelButtonLabel= Cancel
#
#
A property in a properties file is specified in the following format:
Key= Value
In most properties files, you see that the separator between key and value
is the equals sign (=) character. Java also allows use of the colon (:)
character as a separator between key and value, as in: Key:
Value
We won't go into all the details about how Java deals with properties
files, but here are a few general comments on how properties files are
structured: lines starting with an exclamation (!) or a pound (#) character
are ignored when the properties file is read. These characters indicate
that the line is a comment line. Keys are case sensitive. Finally, you can
specify values in a properties file that span multiple lines by using a
backslash ( \ ) to indicate line continuation. Example 4-11 illustrates
this point.
Example 4-11. Properties File Where Values Span More Than One Line
(AnimalResources. properties)
# Sample properties file with keys whose values span more than one line
#
Animals= Cat, Dog, Giraffe, \
Shark, Dolphin, Bird, \
Bear, Moose
If you retrieved the "Animals" key, you'd get back the value Cat,
Dog, Giraffe, Shark, Dolphin, Bird, Bear, Moose. All the leading
whitespace on any continuation line is discarded. You could then, for
example, break up the value returned to you by using the java. util.
StringTokenizer class.
Example 4-12 uses the properties file from Example 4-11 to print a list of
animal names.
Example 4-12. Printing a List of Animals Stored in a Properties Resource
File
import java. util. Locale;
import java. util. ResourceBundle;
import java. util. MissingResourceException;
public class Animals {
public static void main( String [] argv) {
ResourceBundle animalResources;
try {
animalResources = ResourceBundle. getBundle(
"AnimalResources", Locale. getDefault());
System. out. println( animalResources. getString(" Animals"));
} catch (MissingResourceException mre) {
mre. printStackTrace();
}
}
}
Here is the output produced after running Example 4-12:
C:\> java Animals
Cat, Dog, Giraffe, Shark, Dolphin, Bird, Bear, Moose
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|