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)

A Software Engineer Learns Java and Object Orientated Programming
titlepage.xhtml
part0000_split_000.html
part0000_split_001.html
part0000_split_002.html
part0000_split_003.html
part0000_split_004.html
part0000_split_005.html
part0000_split_006.html
part0000_split_007.html
part0000_split_008.html
part0000_split_009.html
part0000_split_010.html
part0000_split_011.html
part0000_split_012.html
part0000_split_013.html
part0000_split_014.html
part0000_split_015.html
part0000_split_016.html
part0000_split_017.html
part0000_split_018.html
part0000_split_019.html
part0000_split_020.html
part0000_split_021.html
part0000_split_022.html
part0000_split_023.html
part0000_split_024.html
part0000_split_025.html
part0000_split_026.html
part0000_split_027.html
part0000_split_028.html
part0000_split_029.html
part0000_split_030.html
part0000_split_031.html
part0000_split_032.html
part0000_split_033.html
part0000_split_034.html
part0000_split_035.html
part0000_split_036.html
part0000_split_037.html
part0000_split_038.html
part0000_split_039.html
part0000_split_040.html
part0000_split_041.html
part0000_split_042.html
part0000_split_043.html
part0000_split_044.html
part0000_split_045.html
part0000_split_046.html
part0000_split_047.html
part0000_split_048.html
part0000_split_049.html
part0000_split_050.html
part0000_split_051.html
part0000_split_052.html
part0000_split_053.html
part0000_split_054.html
part0000_split_055.html
part0000_split_056.html
part0000_split_057.html
part0000_split_058.html
part0000_split_059.html
part0000_split_060.html
part0000_split_061.html
part0000_split_062.html
part0000_split_063.html
part0000_split_064.html
part0000_split_065.html
part0000_split_066.html
part0000_split_067.html
part0000_split_068.html
part0000_split_069.html
part0000_split_070.html
part0000_split_071.html
part0000_split_072.html
part0000_split_073.html
part0000_split_074.html
part0000_split_075.html
part0000_split_076.html
part0000_split_077.html
part0000_split_078.html
part0000_split_079.html
part0000_split_080.html
part0000_split_081.html
part0000_split_082.html
part0000_split_083.html
part0000_split_084.html
part0000_split_085.html
part0000_split_086.html
part0000_split_087.html
part0000_split_088.html
part0000_split_089.html
part0000_split_090.html
part0000_split_091.html
part0000_split_092.html
part0000_split_093.html
part0000_split_094.html
part0000_split_095.html
part0000_split_096.html
part0000_split_097.html
part0000_split_098.html
part0000_split_099.html
part0000_split_100.html
part0000_split_101.html
part0000_split_102.html
part0000_split_103.html
part0000_split_104.html
part0000_split_105.html
part0000_split_106.html
part0000_split_107.html
part0000_split_108.html
part0000_split_109.html
part0000_split_110.html
part0000_split_111.html
part0000_split_112.html
part0000_split_113.html
part0000_split_114.html
part0000_split_115.html
part0000_split_116.html
part0000_split_117.html
part0000_split_118.html
part0000_split_119.html
part0000_split_120.html
part0000_split_121.html
part0000_split_122.html
part0000_split_123.html
part0000_split_124.html
part0000_split_125.html
part0000_split_126.html
part0000_split_127.html
part0000_split_128.html
part0000_split_129.html
part0000_split_130.html
part0000_split_131.html
part0000_split_132.html
part0000_split_133.html
part0000_split_134.html
part0000_split_135.html
part0000_split_136.html
part0000_split_137.html
part0000_split_138.html
part0000_split_139.html