I am trying to use ResourceBundle#getStringArray
to retrieve a String[]
from a properties file. The description of this method in the documentation
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.
A Properties
object can hold Object
s, not just String
s. That tends to be forgotten because they're overwhelmingly used to load .properties files, and so often will only contain String
s. 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.
You can use Commons Configuration, which has methods getList
and getStringArray
that allow you to retrieve a list of comma separated strings.