Grant Wagner
Grant Wagner

Reputation: 25941

How do I specify values in a properties file so they can be retrieved using ResourceBundle#getStringArray?

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?

Upvotes: 35

Views: 66425

Answers (9)

Robert J. Walker
Robert J. Walker

Reputation: 10365

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.

Upvotes: 35

Sujith
Sujith

Reputation: 161

key=value1;value2;value3

String[] toArray = rs.getString("key").split(";");

Upvotes: -1

chrismarx
chrismarx

Reputation: 12575

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;

Upvotes: 1

Lokesh Garg
Lokesh Garg

Reputation: 71

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"}                
    };
}

Upvotes: 2

João Silva
João Silva

Reputation: 91349

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

Upvotes: 7

Murlo
Murlo

Reputation: 117

example:

[email protected], [email protected]

..

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

Upvotes: 1

Yonin
Yonin

Reputation:

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

Upvotes: 0

Alan Krueger
Alan Krueger

Reputation: 4786

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.

Upvotes: 3

Chris Kimpton
Chris Kimpton

Reputation: 5551

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.

Upvotes: 5

Related Questions