Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about Properties

 
George Lin
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • 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
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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 ]
 
Vijay Vaddem
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • 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 Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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 Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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(",");
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic