Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

using ResourceBundle and getStringArray

575 views
Skip to first unread message

Jason Novotny

unread,
Oct 19, 2000, 3:00:00 AM10/19/00
to

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

--
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


Dirk Bosmans

unread,
Oct 22, 2000, 3:00:00 AM10/22/00
to
I'm reacting to following parts of Jason Novotny <nov...@nlanr.net>'s article
in comp.lang.java.programmer on Thu, 19 Oct 2000 16:37:13 GMT6

>
> 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

David M. Karr

unread,
Oct 23, 2000, 3:00:00 AM10/23/00
to
>>>>> "Dirk" == Dirk Bosmans <Dirk.B...@tijd.com> writes:
Dirk> 5. In my Applicet Framework I realized this missing feature of Properties files,
Dirk> and wrote a simple method getStringArray(String array, char delimiter) using a
Dirk> simple loop over a StringTokenizer. I also handle many cases making
Dirk> internationalization very easy.

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)

0 new messages