Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Loading Specific Section of a Properties File

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
How to load specific section of a properties file using java.util.Properties?

Suppose,
I have a properties file as follows (for example):

myproperties.properties:
-----------------------
First=1
Second=2
Third=3

Sum=+
Sub=-
Mul=*

Now my requirement is i have to construct a HashMap which consists of {Sum,+},{Sub,-},{Mul,*}.

Following are some constraints which i have:
1. Keys and Values has to be picked up from my properties file. No hard coding.
2. I should not create a different properties file(To have only Sum,Sub and Mul in it). I have to work with the same properties file.

Please help me out to resolve the problem.

Regds,
Srikanth
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Properties class has no facility for doing anything like this. I suppose you could parse the file yourself, finding the beginning and ending of your section of interest, then create a ByteArrayInputStream from that, and create your Properties object from that stream.

But let me point out that in general, a Hashtable with extra keys is not much of a problem. Lookup is constant-time, so it won't be any slower. You just won't use some of the keys.
 
Master Rancher
Posts: 4588
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given a Properties file with some entries in it, how do you select which ones to copy to the HashMap?

Do you have a list of which ones to select? Or is it hard coded in your program?

Now you have {"Select1", select1} and can add them to the Map

In other words, load the all the props from the file and select the ones you want.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For a slightly less verbose version of what Norm said, try this:

 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic