Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Question about Properties

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,


I am wondering how to use java.util.Properties to get the corresponding value of a specific key whose corresponding value is an array, and I am also wondering how to express/format an array in a properties file. For example, I have a key "friends" whose corresponding value is a String Array {"Tom", "Jerry", "Cat", "Mouse"}. When key "friends" is provided, the corresponding value which I want to get is the String array {"Tom", "Jerry", "Cat", "Mouse"}.


Thanks in advance,
George
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Properties files hold name/value pairs of type String.

From the API Docs:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Properties.html


Because Properties inherits from Hashtable, the put and putAll methods can be applied to a Properties object. Their use is strongly discouraged as they allow the caller to insert entries whose keys or values are not Strings. The setProperty method should be used instead. If the store or save method is called on a "compromised" Properties object that contains a non-String key or value, the call will fail.


[ February 17, 2005: Message edited by: Ben Souther ]
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try like this...

1) in your properties file..

define a key "friends" with values of tom jerry seperated with empty space

friends=Tom Jerry Mouse Cat

2) In your class...
get the key as a normal string
String friends = (String) props.get("friends");

3) construct a StringTokenizer class with a empty space delimiter...
and get the values...

For StringTokenizer, check
this

Let us know if you have any problems...

vijay
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

3) construct a StringTokenizer class with a empty space delimiter...
and get the values...



With Java 1.4 and up, you can also use the String.split method which returns an array.
 
Vijay Vaddem
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

With Java 1.4 and up, you can also use the String.split method which returns an array.



Oops..... Oh yes.... Im trying to catch you Ben

Kind Regards,
Vijay
 
George Lin
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Vijay,


Originally posted by Vijay Vaddem:
Try like this...

1) in your properties file..

define a key "friends" with values of tom jerry seperated with empty space

friends=Tom Jerry Mouse Cat

2) In your class...
get the key as a normal string
String friends = (String) props.get("friends");

3) construct a StringTokenizer class with a empty space delimiter...
and get the values...

For StringTokenizer, check
this

Let us know if you have any problems...

vijay



Your reply is very helpful. I am wondering whether there exists a Java built-in API to load a String array from a Java property file directly.


regards,
George
 
George Lin
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ben,


Originally posted by Ben Souther:
Properties files hold name/value pairs of type String.

From the API Docs:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Properties.html


[ February 17, 2005: Message edited by: Ben Souther ]



I do not quite understand what is your meaning. Do you mean Properties class can only load String as value, so String[] can not be loaded directly?


regards,
George
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In short yes.
As Vijay mentioned, there is nothing stopping you from adding a delimited string and tokenizing it into an array after you remove it from the properties object.

names = John,Mary,Jane,Jim,Joe

String[] names = (myProperties.getProperty("names")).split(",");
 
He baked a muffin that stole my car! And this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic