This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Java in General and the fly likes doubt in properties Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "doubt in properties" Watch "doubt in properties" New topic
Author

doubt in properties

Thyagarajan Ramanujam
Greenhorn

Joined: Mar 24, 2008
Posts: 25
I have a file which has values of the properties for my java class like

name="peter";
age=20;
named as, say details.txt


Is there any simple methods in java API to get the values of name and age.

Or Should i use java.io package like File, FileReader classes. As far as I studied, these classes will read the content of the file but i want to know if there are some methods which will return the value if the name of the property is passed.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Hi!

For config files with name/value pairs like yours you could use the "java.util.Property" class. Here's a simple code fragment for your problem:



You have to load your config file as shown above. The getProperty() method returns a String containing the value for the key given as parameter. Additionally you can provide a default value as second parameter to the overloaded getProperty() method which will be used if there is no key with the name you provided in your config file!

And because getProperty() only returns String objects you have to parse it to get the numeric value like i did above with "age".

Greetings
Marco
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: doubt in properties