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!


Recommended Answers

All 7 Replies

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

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!

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.