Getting started
You will begin by creating a properties file for a set of messages that will appear in a program that performs basic arithmetic. Start by creating an Eclipse project, and add a file called messages.properties to the src folder by right clicking on the “src” folder and choosing New -> File. Add the following content to the file:
introduction.message=Welcome to the adder program
input.message=The input values are {0} and {1}
result.message=The result is {0}
This file contains three properties. On the key side of each property you should use dots rather than spaces, and I prefer to only use lower case characters.
You can see that two of these property values contain special character sequences, for instance, {1}. These are placeholders for values that will be provided at runtime.
Now, add a class with a main method called Adder:
package adder;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Properties;
public class Adder {
private Properties properties = new Properties();
public Adder() {
try {
properties.load(getClass().getResourceAsStream(
"/messages.properties"));
System.out.println(properties.getProperty(
"introduction.message"));
} catch (IOException e) {
e.printStackTrace(System.out);
}
}
public void add(int a, int b) {
String inputMessage = properties.getProperty(
"input.message");
String resultMessage = properties.getProperty(
"result.message");
System.out.println(MessageFormat.format(
inputMessage, a, b));
System.out.println(MessageFormat.format(
resultMessage, a + b));
}
public static void main(String[] args) {
Adder adder = new Adder();
adder.add(20, 30);
adder.add(23, 12);
}
}
When the Adder class is constructed it populates a Properties object from the values in messages.properties. The Properties object is essentially a Map: it is possible to get and put properties based on their key.
The load method simply accepts an InputStream. Although it would be possible to use a FileInputStream, and alternative approach has been used: Because the properties file is in the src directory, it is copied to the bin directory when the program is compiled (along with the class file). It is therefore possible to use the following line of code to create an InputStream backed by the properties file:
getClass().getResourceAsStream("/messages.properties")
The getClass method returns a representation of the object’s class as a Class object. You can use a helper method on this (and every Class) called getResourceAsStream. This method can be used to read any resource on the classpath – and because messages.properties is in the root directory of the classpath, you add a leading forward slash.
You may remember from earlier in the book that the classpath is the collection of resources (primarily classes) available to a program when it executes.
If you had used a FileInputStream you would need to know the location of the file at runtime.
Once the properties file is loaded you can begin to use it as follows:
properties.getProperty("introduction.message")
The value is always returned as an instance of String.
The use of the properties in the add method is slightly more complex because you need to provide values for the placeholders provided in the message. In order to achieve this, you use the MessageFormat helper class. This accepts a String with placeholders as its first parameter, and the values required for the placeholders in the remaining parameters:
MessageFormat.format(inputMessage, a, b)