I am trying to use ResourceBundle#getStringArray
to retrieve a String[]
from a properties file. The description of this method in the documentation
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;
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;
}
key=value1;value2;value3
String[] toArray = rs.getString("key").split(";");
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"}
};
}
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;
}
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.