| Author |
Question about Properties
|
George Lin
Ranch Hand
Joined: Jan 11, 2005
Posts: 125
|
|
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
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
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 ]
|
Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
|
 |
Vijay Vaddem
Ranch Hand
Joined: Feb 13, 2004
Posts: 243
|
|
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
Joined: Dec 11, 2004
Posts: 13410
|
|
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
Joined: Feb 13, 2004
Posts: 243
|
|
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
Joined: Jan 11, 2005
Posts: 125
|
|
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
Joined: Jan 11, 2005
Posts: 125
|
|
Thanks Ben,
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
Joined: Dec 11, 2004
Posts: 13410
|
|
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(",");
|
 |
 |
|
|
subject: Question about Properties
|
|
|