Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Loading Specific Section of a Properties File

 
S Srikanth
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Norm Radder
Ranch Hand
Posts: 882
6
  • Mark post as helpful
  • send pies
  • 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.
 
Robert Watkins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a slightly less verbose version of what Norm said, try this:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic