wood burning stoves*
The moose likes Beginning Java and the fly likes How to get multiple values using System.getProperty Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to get multiple values using System.getProperty" Watch "How to get multiple values using System.getProperty" New topic
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: 19656
    
  18

Alternatively, loop until no value is found:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
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: 19656
    
  18

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: 91
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get multiple values using System.getProperty
 
Similar Threads
Environment Variable!!
Please Help : Need Help on Threading
Property
Better approach if i have multiple datasources JNDIObjectFactoryBean or JVM Variables
How to set virtual arguments in servlet