Read the properties file first, and then use Messageformat to format the string.
The Messageformat-java.text.messageformat class allows you to replace a portion of a message string (in this case, a message from a resource bundle) with the parameters specified by the runtime. This is useful in situations where you create a sentence, but the words appear in different order in different languages. The placeholder string {0} in the message is replaced with the first run-time parameter, {1} is replaced with the second run-time parameter, and so on.
To do this, you must first create two resource bundles
The format of this particular message is defined as "{0} while loading: {1}" and the second bundle is in the format "{1} loaded unsuccessfully: {0}". {0} represents a placeholder for the argument to be substituted for the message. Placeholders start at 0 and increase.
Use the Messageformat.format () method when you replace the argument and actually create the message to display. This takes two arguments, the first is the message to be formatted, and the second is the argument Object []. The following shows all the code:
Import java.text.*;
public class Format-it {
public static void Main (String args[]) {
String format1 = "{0} while loading: {1}";
String Format2 = "{1} loaded unsuccessfully: {0}";
String exceptionname = "I/o Exception";
String filename = "Foobar.java";
Object [] Fmtargs = {exceptionname, filename};
System.out.println (
Messageformat.format (FORMAT1, Fmtargs));
System.out.println (
Messageformat.format (FORMAT2, Fmtargs));
}
}
The output that runs is as follows:
Foobar.java while LOADING:I/O Exception
I/O Exception loaded Unsuccessfully:Foobar.java