Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get multiple values using System.getProperty

 
Tariq Ahsan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternatively, loop until no value is found:
 
Tariq Ahsan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then looping through the property names is the best option, using String.startsWith to check for matches.
 
Irina Goble
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic