954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Reading .properties file with parameters

Hy,

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!

BR,
end3r

end3r
Light Poster
37 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

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

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 
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.

end3r
Light Poster
37 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

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.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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!

end3r
Light Poster
37 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

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

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 
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".

end3r
Light Poster
37 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

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.

end3r
Light Poster
37 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: