Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 791
Grant Wagner Avatar asked Oct 22 '08 14:10

Grant Wagner


People also ask

Where does ResourceBundle properties file go?

ResourceBundle property files contain locale-specific objects for use by Java classes. The ResourceBundle property file must be placed somewhere in the CLASSPATH . Typically this is best accomplished by placing the ResourceBundle properties file in the same directory as the gear message class that it maps to.

What is ResourceBundle in spring?

Spring's application context is able to resolve text messages for a target locale by their keys. Typically, the messages for one locale should be stored in one separate properties file. This properties file is called a resource bundle. MessageSource is an interface that defines several methods for resolving messages.


2 Answers

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.

like image 158
Robert J. Walker Avatar answered Oct 11 '22 03:10

Robert J. Walker


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

like image 38
João Silva Avatar answered Oct 11 '22 02:10

João Silva