views:

2605

answers:

5

I am trying to use ResourceBundle#getStringArray to retrieve a String[] from a properties file. The description of this method in the documentation reads:

Gets a string array for the given key from this resource bundle or one of its parents.

However, I have attempted to store the values in the properties file as multiple individual key/value pairs:

key=value1
key=value2
key=value3

and as a comma-delimited list:

key=value1,value2,value3

but neither of these is retrievable using ResourceBundle#getStringArray.

How do you represent a set of key/value pairs in a properties file such that they can be retrieved using ResourceBundle#getStringArray?

+2  A: 

Umm, looks like this is a common problem, from threads here and here.

It seems either you don't use the method and parse the value for an array yourself or you write your own ResourceBundle implementation and do it yourself :(. Maybe there is an apache commons project for this...

From the JDK source code, it seems the PropertyResourceBundle does not support it.

Chris Kimpton
I had done some googling as well, but none of the answers seemed to specifically state that PropertyResourceBundle does not support this. I think I saw an apache commons project that provides a getStringArray() method to handle key=value1,value2,value3, but I'll just roll my own. Thanks.
Grant Wagner
+6  A: 

A Properties object can hold Objects, not just Strings. That tends to be forgotten because they're overwhelmingly used to load .properties files, and so often will only contain Strings. The documentation indicates that calling bundle.getStringArray(key) is equivalent to calling (String[]) bundle.getObject(key). That's the problem: the value isn't a String[], it's a String.

I'd suggest storing it in comma-delimited format and calling split() on the value.

Robert J. Walker
Thanks, the answer you provided is what I suspected. I'd already implemented something using split(), I was just hoping I could leverage something that was already in the class library, rather than rolling my own.
Grant Wagner
A: 

I don't believe this is possible with ResourceBundles loaded from a properties file. The PropertyResourceBundle leverages the Properties class to load the properties file. The Properties class loads a properties file as a set of String->String map entries and doesn't support pulling out String[] values.

Calling ResourceBundle.getStringArray just calls ResourceBundle.getObject, casting the result to a String[]. Since the PropertyResourceBundle just hands this off to the Properties instance it loaded from the file, you'll never be able to get this to work with the current, stock PropertyResourceBundle.

Alan Krueger
A: 
public String[] getPropertyStringArray(PropertyResourceBundle bundle, String keyPrefix) {
    String[] result;
    Enumeration<String> keys = bundle.getKeys();
    ArrayList<String> temp = new ArrayList<String>();

    for (Enumeration<String> e = keys; keys.hasMoreElements();) {
     String key = e.nextElement();
     if (key.startsWith(keyPrefix)) {
      temp.add(key);
     }
    }
    result = new String[temp.size()];

    for (int i = 0; i < temp.size(); i++) {
     result[i] = bundle.getString(temp.get(i));
    }

    return result;
}
+2  A: 

You can use Commons Configuration, which has methods getList and getStringArray that allow you to retrieve a list of comma separated strings.

JG
@JG: Thanks, I'll consider Commons Configuration on my next project. I like that it supports `key : value1`, `key : value2`, `key : value3` on multiple lines as well as a comma-separated list (and the ability to escape commas in the value, which my roll-your-own version doesn't support).
Grant Wagner