34

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?

Grant Wagner
  • 25,263
  • 7
  • 54
  • 64

9 Answers9

34

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.

josliber
  • 43,891
  • 12
  • 98
  • 133
Robert J. Walker
  • 10,027
  • 5
  • 46
  • 65
  • 2
    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 Oct 22 '08 at 15:17
  • Thanks for the answer! I used a simple split that picked up the spacing I used after the commas (for readability). I had to use a more sophisticated split like: http://stackoverflow.com/questions/1396084/regex-for-comma-delimited-list – ChrisCantrell Jan 14 '14 at 14:15
7

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

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
João Silva
  • 89,303
  • 29
  • 152
  • 158
  • 1
    @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 Aug 18 '09 at 21:11
  • Does this work for property files only, or could this work for Java "-D" command line property parameters too? – David Nov 26 '13 at 00:46
5

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
  • 5,546
  • 6
  • 45
  • 72
  • 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 Oct 22 '08 at 15:20
3

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
  • 4,701
  • 4
  • 35
  • 48
2

I have tried this and could find a way. One way is to define a subclass of ListresourceBundle, then define instance variable of type String[] and assign the value to the key.. here is the code

@Override
protected Object[][] getContents() {
    // TODO Auto-generated method stub

    String[] str1 = {"L1","L2"};

    return new Object[][]{

            {"name",str1},
            {"country","UK"}                
    };
}
Lokesh Garg
  • 71
  • 1
  • 1
  • 5
1

example:

mail.ccEmailAddresses=he@anyserver.at, she@anotherserver.at

..

myBundle=PropertyResourceBundle.getBundle("mailTemplates/bundle-name", _locale);

..

public List<String> getCcEmailAddresses() 
{
    List<String> ccEmailAddresses=new ArrayList<String>();
    if(this.myBundle.containsKey("mail.ccEmailAddresses"))
    {
        ccEmailAddresses.addAll(Arrays.asList(this.template.getString("mail.ccEmailAddresses").split("\\s*(,|\\s)\\s*")));// 1)Zero or more whitespaces (\\s*) 2) comma, or whitespace (,|\\s) 3) Zero or more whitespaces (\\s*)
    }       
    return ccEmailAddresses;
}
Murlo
  • 117
  • 1
  • 2
1

just use spring - Spring .properties file: get element as an Array

relevant code:

base.module.elementToSearch=1,2,3,4,5,6

@Value("${base.module.elementToSearch}")
  private String[] elementToSearch;
Community
  • 1
  • 1
chrismarx
  • 11,488
  • 9
  • 84
  • 97
0
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;
}
-1
key=value1;value2;value3

String[] toArray = rs.getString("key").split(";");
Sujith
  • 141
  • 2
  • 14