I've been successfully using the ResourceBundle class to mimic the
functionality of a Properties() file, except that I don't need to
specify whre my properties file is, as long as it's in the classpath.
Basically I have a file called foo.properties with the following, say:
foobar=banana
Then I can do:
ResourceBundle configBundle = ResourceBundle.getBundle("foo.foo");
String foobar = configBundle.getString("foobar");
and all is well. However, I want to add multiple values for the same
key, and I'm not sure what the appropiate syntax is:
foobar=banana
foobar=apple
.....
and I want to do something like
String[] foobar = configBundle.getStringArray("foobar")
but this doesn't seem to work- I've tried to find ny info on using the
getStringArray() method on the web without any luck.
Any information on this is greatly appreciated
Thanks, Jason
--
Jason Novotny nov...@nlanr.net
Home: (510) 704-9917 Work: (510) 486-8662
NLANR Applications Support http://dast.nlanr.net
NERSC Distributed Computing http://www-didc.lbl.gov
>
> Hello,
>
> I've been successfully using the ResourceBundle class to mimic the
> functionality of a Properties() file, except that I don't need to
> specify whre my properties file is, as long as it's in the classpath.
>
> Basically I have a file called foo.properties with the following, say:
>
> foobar=banana
>
> Then I can do:
>
> ResourceBundle configBundle = ResourceBundle.getBundle("foo.foo");
>
> String foobar = configBundle.getString("foobar");
>
> and all is well. However, I want to add multiple values for the same
> key, and I'm not sure what the appropiate syntax is:
>
> foobar=banana
> foobar=apple
> .....
>
> and I want to do something like
>
> String[] foobar = configBundle.getStringArray("foobar")
>
> but this doesn't seem to work- I've tried to find ny info on using the
> getStringArray() method on the web without any luck.
>
> Any information on this is greatly appreciated
>
> Thanks, Jason
1. As PropertyResourceBundle uses a Properties object, which in turn is a
subclass of Hashtable, adding foobar=apple after foobar=banana will replace the
value banana for the key foobar by the value apple.
2. A ResourceBundle maps String keys to Object values. The getStringArray(String
key) method is just shorthand for <CODE>return (String[])getObject(key);</CODE>
3. PropertyResourceBundle is a subclass for ResourceBundle that handles only
String-type values, so the cast to String[] in getStringArray will throw a
ClassCastException (which may be handled in the method itself).
4. A ListResourceBundle may be subclassed to return any Object type, including a
String[]
5. In my Applicet Framework I realized this missing feature of Properties files,
and wrote a simple method getStringArray(String array, char delimiter) using a
simple loop over a StringTokenizer. I also handle many cases making
internationalization very easy.
Greetings,
Dirk Bosmans
http://users.belgacombusiness.net/arci/
- Applicet Framework: turns Applets into Applications
- ArciMath BigDecimal: now with BigDecimalFormat
I implemented something very similar in my own framework. I assume
the delimiter is "|", but that would be easy to softcode. The other
little annoyance is dealing with "abc||def". Logically, this should
represent a three-element array, with the second being an empty
string. Unfortunately, StringTokenizer gets confused when it finds
two delimiters right next to each other. The alternatives are
preprocessing a string expected to have array delimiters in it, adding
spaces to those anomalous elements, or writing a different version of
StringTokenizer that can deal properly with empty elements.
--
===============================================================================
David M. Karr ; dk...@tcsi.com ; w:(425)487-8312 ; TCSI & Best Consulting
Software Engineer ; Unix/Java/C++/X ; BrainBench CJ12P (#12004)