18.2 Properties Files

Applications need to customize their behavior and access information about the runtime environment. This could be about getting configuration data to run the application, tailoring the user interface to a specific locale, accessing the particulars for a database connection, customizing colors and fonts, and many other situations. It is not a good idea to hard-code such information in the application, but instead making it available externally so that the application can access it when needed, and where it can be modified without having to compile the application code.

Creating a Property List in a Properties File

A property list contains key–value pairs that designate properties, where a key is associated with a value, analogous to the entries in a map. Such a list can be created in a properties file, where a key–value pair is defined on each line according to the following syntax:

<
key
> = <
value
>

Following are some examples of properties that define information about the Java SE 17 Developer exam:

Click here to view code image

cert.name = OCP, Java SE 17 Developer
exam.title = Java SE 17 Developer
exam.number = 1Z0-829

Alternatively, we can use the colon (:) instead of the equals sign (=). In fact, whitespace can also be used to separate the key and the value. Any leading whitespace on a line is ignored, as is any whitespace around the equals sign (=).

Both the key and the value in a properties file are interpreted as String objects. The value string comprises all characters on the right-hand side of =, beginning with the first non-whitespace character and ending with the last non-whitespace character. Here are some examples:

Click here to view code image

company = GLOBUS
greeting=Hi!
        gratitude              =Thank you!

We do not need to escape the metacharacter = or : in a value string. However, key names can be specified by escaping any whitespace in the name with the backslash character (\). Metacharacters = and : in a key name can also be escaped in the same way. In the first example below, the key string is “fake smiley” and the value string is “:=)”. In the second example below, the key string is “:=)” and the value string is “fake smiley”.

fake\ smiley = :=)
\:\=) = fake smiley

A backslash (\) at the end of a line can also be used to break the property specification across multiple lines, and any leading whitespace on a continuation line is ignored. The value in the following property specification is “Au revoir!” and not “Au revoir!”:

Click here to view code image

“Au            revoir!”:
farewell = Au \
           revoir!

If only the key is specified and no value is provided, the empty string (“”) is returned as the value, as in the following example:

KeyWithNoValue

If necessary, any conversion of the value string must be explicitly done by the application.

In the case of a duplicate key in a properties file, the last key–value pair with a duplicate key supersedes any previous resource specifications with this key.

If the first non-whitespace character on a line is the hash sign (#), the whole line is treated as a comment, and thereby ignored—as are blank lines. The exclamation mark (!) can also be used for this purpose.

# Any comments so far?

If the character encoding of a properties file does not support Unicode characters, then these must be specified using the \uxxxx encoding in the file. For example, to specify the property euro = €, we can encode it as:

euro = \u20AC

Since a properties file is a text file, it can conveniently be created in a text editor, and appropriate file permissions set to avoid unauthorized access. Although the name of a properties file can be any valid file name, it is customary to append the file extension .properties.

The Java Standard library provides classes to utilize the properties defined in a properties file.

  • The java.util.ResourceBundle class can be used to implement a resource bundle based on the properties in a property (resource) file (p. 1102).
  • The java.util.Properties class can be used to implement a Properties table based on the properties defined in a properties file—but this will not be discussed here, as this class is beyond the scope of this book.

Leave a Reply

Your email address will not be published. Required fields are marked *