• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get multiple values using System.getProperty

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alternatively, loop until no value is found:
 
Tariq Ahsan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then looping through the property names is the best option, using String.startsWith to check for matches.
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic