• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt in properties

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Their achilles heel is the noogie! Give them noogies tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic