I have a .properties file with a key-value defined like this: my_property.value=text text text bla bla bla {0} qwerty {1}. I need to know what class should I use if I want to read the value and insert parameters in {0} and {1}. In the past I used to work with this, but I cannot remember what class I used to solve this.

Thanks in advance!


6 Years
Discussion Span
Last Post by end3r
Featured Replies
  • 1
    ~s.o.s~ 2,560   6 Years Ago

    Look into the java.text.MessageFormat class. Also, [URL="http://stackoverflow.com/questions/495776/should-i-use-java-text-messageformat-for-localised-messages-without-placeholders"]read this[/URL]. Read More


Look at the Properties class. It has methods to read properties files.

I looked at the Properties class. It has no method that would allow me to get the value using parameters.
The method I used was something like:

String getPropertyValue(String key, String..params){}

I need to know if it was something in java, or a framework, or it was just part of that past project.


Could you show a line from your properties and then show variables with the parsed out values from that line.
It may take more that one line of code if you have a custom properties file.


user.authenticate.message.success={0} logged in successfully!

String key = "user.authenticate.message.success";
//Here would be the method call (getValue is just a random name because i don't know exactly the original one):
String value = getValue(key, "John");

//and the result would be "John logged in successfully!"

If there is no method already defined that does that, I could write the method myself, so it's no use to post code for solving my problem.

Thx for your quick response!

Edited by end3r: n/a


Look into the java.text.MessageFormat class. Also, read this.

I found it. It's a library from apache.
The class is org.apache.commons.resources.Messages.
Now I have to instantiate it by using the following line of code:

Messages msg = new Messages(new ResourceBundleResources("name", "base"));

Although I read the documentation I still don't know how to set the path to my .properties file. I don't know what should I put in "name" and "base".


Thank you ~S.0.S.~ for pointing the MessageFormat class.
After trying to use the apache classes, I must admit that the MessageFormat is the best approach, at least for a simple desktop application written in pure Swing.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.