The moose likes Java in General and the fly likes Loading Specific Section of a Properties File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Loading Specific Section of a Properties File" Watch "Loading Specific Section of a Properties File" New topic

Loading Specific Section of a Properties File

S Srikanth

Joined: Sep 09, 2002
Posts: 14
How to load specific section of a properties file using java.util.Properties?

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


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.

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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.

[Jess in Action][AskingGoodQuestions]
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 694
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

Joined: Sep 20, 2005
Posts: 25
For a slightly less verbose version of what Norm said, try this:

Software is too expensive to build cheaply...
I agree. Here's the link:
subject: Loading Specific Section of a Properties File
It's not a secret anymore!