Need to escape the [ at the starting of a value.
I am using PropertyResourceBundle to read the properties file and i have a property whose value is starting with a square bracket like
myapp.add.user.email.selfRegistration.subject=[MYAPP] Welcome to MYAPP
when i try to read this file i get following exception
java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to
java.lang.String at
java.util.ResourceBundle.getString(ResourceBundle.java:355)
i am using jdk7
java.util.PropertyResourceBundle
is based on the java.util.Properties
. Technically java.util.Properties
implements Map<Object,Object>
but when you load properties from a file the keys and values are restricted to String
only (check the source of the java.util.Properties.load()
methods). And the '[' character has no special meaning in the properties file format.
Hence it is impossible to get ClassCastException
due to attempted cast of String[]
to String
if all your resource bundles are indeed based on property files.
Most likely you have a ListResourceBundle
-based bundle (or a custom subclass of ResourceBundle
) which can contain values of any type, arrays included.
And it is possible to mix property-based and class-based resources bundles with the same base name, for example the default resource bundle can be properties file while locale-specific child bundles are ListResourceBundle
-based.
I tried the same with the below program and i am able to get the output from my program without any exception
can you check it and let me know if any other issues
package com.kb;
import java.util.Enumeration;
import java.util.ResourceBundle;
public class ResourceBundleTest {
public static void main(String[] args) {
ResourceBundle rb = ResourceBundle.getBundle("mybundle");
Enumeration <String> keys = rb.getKeys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
String value = rb.getString(key);
System.out.println(key + ": " + value);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.