aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
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
Author

Loading Specific Section of a Properties File

S Srikanth
Greenhorn

Joined: Sep 09, 2002
Posts: 14
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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: 687
    
    1
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

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...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Loading Specific Section of a Properties File