How to specify values โ€‹โ€‹in a properties file so that they can be retrieved using ResourceBundle # getStringArray?

I am trying to use ResourceBundle#getStringArray to retrieve String[] from a properties file. The description of this method in the documentation reads:

Gets a string array for a given key from this resource package or one of its parents.

However, I tried to save the values โ€‹โ€‹in the properties file as several separate key / value pairs:

 key=value1 key=value2 key=value3 

and as a comma separated list:

 key=value1,value2,value3 

but none of them can be restored using ResourceBundle#getStringArray .

How do you represent a set of key / value pairs in a properties file so that they can be found using ResourceBundle#getStringArray ?

+24
java resourcebundle
Oct 22 '08 at 14:43
source share
9 answers
An object

A Properties can contain Object s , not just String s. This is usually forgotten because they are overwhelmingly used to download .properties files and therefore often only contain String s. The documentation states that calling bundle.getStringArray(key) equivalent to calling (String[]) bundle.getObject(key) . What is the problem: the value is not String[] , it is a String .

I would suggest saving it in comma-separated format and calling split() for the value.

+31
Oct. 22 '08 at 15:03
source share

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

+7
Aug 18 '09 at 0:57
source share

Umm, this seems like a common problem, out of threads here and there.

It seems that you either do not use the method, either parse the value for the array yourself, or write your own implementation of ResourceBundle and do it yourself :( Perhaps there is an apache commons project for this ...

From the JDK source code, it seems that PropertyResourceBundle does not support it.

+3
Oct 22 '08 at 15:01
source share

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; } 
+2
Feb 04 '13 at 16:49
source share

I do not think this is possible with ResourceBundles loaded from the properties file. The PropertyResourceBundle property uses the Properties class to load the properties file. The Properties class loads the properties file as a set of entries String-> String and does not support drawing String [] values.

Calling ResourceBundle.getStringArray simply calls ResourceBundle.getObject, discarding the result on String []. Because PropertyResourceBundle simply passes this value to the property instance that it loaded from the file, you can never get it to work with the current PropertyResourceBundle.

+1
Oct 22 '08 at 15:06
source share

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

corresponding code:

 base.module.elementToSearch=1,2,3,4,5,6 @Value("${base.module.elementToSearch}") private String[] elementToSearch; 
+1
Apr 27 '15 at 16:55
source share
 key=value1;value2;value3 String[] toArray = rs.getString("key").split(";"); 
+1
May 12 '15 at 3:29
source share
 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; } 
0
Oct 23 '08 at 11:08
source share

I tried this and could find a way. One way is to define a subclass of ListresourceBundle, then define an instance variable of type String [] and assign a 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"} }; } 
0
Dec 27 '14 at 1:56
source share



All Articles