| Author |
How to get multiple values using System.getProperty
|
Tariq Ahsan
Ranch Hand
Joined: Nov 03, 2003
Posts: 116
|
|
Hello, I am using an Unix environment which has the old JDK 1.3. I am trying to use System.getProperty to read a configuration file where I want to fetch all values that partially matches with a certain key. For example the config file would have - key.type.1=value1 some.type.1=value2 key.type.2=value3 I want to get both the values of 'key.type.1' and 'key.type.2' where the call of the getProperty method is like - Properties pProperties = new Properties(); pProperties.getProperty("key.type"); What would be the best way to get a List of the above two key values? Thanks Tariq
|
 |
Stevi Deter
Ranch Hand
Joined: Mar 22, 2008
Posts: 265
|
|
Tariq, Have you checked out the Java Tutorial on Properties? One possible approach is to use Properties#propertyNames to get an Enumeration of the property names available, find the ones that meet your criteria, and get those properties. [ April 24, 2008: Message edited by: Stevi Deter ]
|
There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Alternatively, loop until no value is found:
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Tariq Ahsan
Ranch Hand
Joined: Nov 03, 2003
Posts: 116
|
|
Thanks for your suggestions. Actually I should have the example key value as - key.type.ABC=value1 instead of key.type.1=value1
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Then looping through the property names is the best option, using String.startsWith to check for matches.
|
 |
Irina Goble
Ranch Hand
Joined: May 09, 2004
Posts: 75
|
|
Tariq, you can load property names in a sorted set and then use the subSet() method to get just the ones you need. Here is an example: Edit: Oops, missed the Java version, changed the example for 1.3. [ April 27, 2008: Message edited by: Irina Goble ] [ April 27, 2008: Message edited by: Irina Goble ]
|
 |
 |
|
|
subject: How to get multiple values using System.getProperty
|
|
|