Paul Labis: This is a personal blog where everyone can read my experiences, ideas and thoughts about programming, latest gadgets and other information technology related topics, lifestyle and many other stuff I would like to share to the public.

Use Arguments on Property File

The goal on this tutorial is to be able to format a value on a properties file given its parameter or arguments. It is best describe as passing value as a parameter and inserting those values on the string retrieved from the properties file.

To better understand what I'm doing, read and understand my example code below.

Property File Sample
message.welcome= Welcome {0} to {1}!
message.thank= Thank you for you visit {0}.
Java Code using  ResourceBundle and MessageFormat
ResourceBundle resBundle = new ResourceBundle("path to property file");
String welcomeText=resBundle.getString("message.welcome");
MessageFormat msgFormatter = new MessageFormat(welcomeText);

// Create the dynamic args you want to replace
Object[] messageArguments = {"Techie boy","Philippines"}; 

String finalText= msgFormatter.format(messageArguments);

The finalText will result to "Welcome Techie boy to Philippines!"