How to use UTF-8 in resource properties with ResourceBundle

java resource bundle properties file location
convert properties file to utf-8
resourcebundle vs properties
java resource bundle multiple properties files
java properties utf-8
java resource bundle best practices
resource bundle classpath
resourcebundle java 8

I need to use UTF-8 in my resource properties using Java's ResourceBundle. When I enter the text directly into the properties file, it displays as mojibake.

My app runs on Google App Engine.

Can anyone give me an example? I can't get this work.

PropertyResourceBundle (Java SE 9 & JDK 9 ), The following example shows a member of a resource bundle family with the base name are the same as the keys you use in your program to retrieve the objects from the bundle. This constructor reads the property file in UTF-8 by default. UTF-8 handling for ResourceBundle and Properties Every new version of the JDK comes with small improvements, often unnoticed, that however correct long-standing bugs or request for enhancements. One such improvements is - finally! - the ability for java.util.Properties to handle non ISO-8859-1 properties files.

Given that you have an instance of ResourceBundle and you can get String by:

String val = bundle.getString(key); 

I solved my Japanese display problem by:

return new String(val.getBytes("ISO-8859-1"), "UTF-8");

PropertyResourceBundle (Java SE 9 & JDK 9 ), The following example shows a member of a resource bundle family with the base name are the same as the keys you use in your program to retrieve the objects from the bundle. This constructor reads the property file in UTF-8 by default. If the system property java.util.PropertyResourceBundle.encoding is set to either "ISO-8859-1" or "UTF-8", the input stream is solely read in that encoding, and throws the exception if it encounters an invalid sequence. Other encoding values are ignored for this system property.

look at this : http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html#load(java.io.Reader)

the properties accept an Reader object as arguments, which you can create from an InputStream.

at the create time, you can specify the encoding of the Reader:

InputStreamReader isr = new InputStreamReader(stream, "UTF-8");

then apply this Reader to the load method :

prop.load(isr);

BTW: get the stream from .properties file :

 InputStream stream = this.class.getClassLoader().getResourceAsStream("a.properties");

BTW: get resource bundle from InputStreamReader:

ResourceBundle rb = new PropertyResourceBundle(isr);

hope this can help you !

A Guide to the ResourceBundle, This article covers how to use the ResourceBundle to cope with the The first thing we should know is that all files within one resource bundle must be in the native2ascii -encoding UTF-8 utf8.properties nonUtf8.properties� Right, you can create a custom ResourceBundle or use the native2ascii converter (if necessary with the Maven 2 plugin to make the conversion more transparent). Since the other answer only goes with the last approach in detail, here's another answer how you could create a custom ResourceBundle to load properties files as UTF-8 in a JSF 2.x application on Java SE 1.6 based environment.

ResourceBundle.Control with UTF-8 and new String methods don't work, if the properties file uses cp1251 charset, for example.

So I recomended using a common method: write in unicode symbols. For this:

IDEA -- has a special "Transparent native-to-ASCII conversion" option (Settings > File Encoding).

Eclipse -- has a plugin "Properties Editor". It can work as separate application.

Using Unicode in Java Resource Bundles – Software , Creating a UTF-8 Enabled ResourceBundle Control we can easily tell the bundle loader to use UTF-8 for the property file charset encoding. It follows the basic ResourceBundle rule of not specifying file extension or language codes, but can refer to any Spring resource location (instead of being restricted to classpath resources). With a "classpath:" prefix, resources can still be loaded from the classpath, but "cacheSeconds" values other than "-1" (caching forever) will not work

This problem has finally been fixed in Java 9: https://docs.oracle.com/javase/9/intl/internationalization-enhancements-jdk-9

Default encoding for properties files is now UTF-8.

Most existing properties files should not be affected: UTF-8 and ISO-8859-1 have the same encoding for ASCII characters, and human-readable non-ASCII ISO-8859-1 encoding is not valid UTF-8. If an invalid UTF-8 byte sequence is detected, the Java runtime automatically rereads the file in ISO-8859-1.

UTF-8 handling for ResourceBundle and Properties, UTF-8 handling for ResourceBundle and Properties loadFromXML( InputStream), that allowed to specify properties using an XML syntax, and the kind of resource to load (a Java class, a properties file, a properties file in� How to use UTF-8 in resource properties with ResourceBundle (10) I need to use UTF-8 in my resource properties using Java's ResourceBundle. When I enter the text directly into the properties file, it displays as mojibake. My app runs on Google App Engine. Can anyone give me an example?

Utf8ResourceBundle.java � GitHub, UTF-8 friendly ResourceBundle support Use: ResourceBundle bundle = Utf8ResourceBundle. @return Unicode friendly property resource bundle. */. In this tutorial we will name our config file to crunchify.properties. We are going to use public static final ResourceBundle getBundle(String baseName) Above method gets a resource bundle using the specified base name, the default locale, and the caller's class loader.

JEP 226: UTF-8 Property Resource Bundles, This format is supported by the standard resource-bundle lookup mechanism. As noted in the related RFEs, this format is difficult to use because it requires continuous class to load properties files from ISO-8859-1 to UTF-8. The library reads a property file based on the locale and name suffix used in the naming of the property. For example, consider a label file named MyLabels_en_US.properties. This file is read by ResourceBundle utility when the Locale is set to en_US which stands for US-English. Locale represents a region or location with its attributes.

java.util.PropertyResourceBundle java code examples, How to use UTF-8 in resource properties with ResourceBundle. URL url = loader. getResource(resourceName); if (url != null) { URLConnection connection = url.

Comments
  • Java 1.6 Fixed this as you can pass in a Reader. See the @Chinaxing answer way down below
  • @Will: question is primarily about reading them via java.util.ResourceBundle, not java.util.Properties.
  • Check this answered question,,, hope it helps you [stackoverflow.com/questions/863838/… [1]: stackoverflow.com/questions/863838/…
  • JDK9 should support UTF-8 natively, see JEP 226
  • Thanks. BTW it seems to be a good idea to override getFormats to return FORMAT_PROPERTIES.
  • Could you elaborate on this suggestion to override getFormats()?
  • @imgx64: Thank you for notifying. Answer has been fixed.
  • Do not hesitate to use StandardCharsets.UTF_8 if your're using Java 7+
  • @Nyerguds: if you see reasons to ever programmatically change it (I can't for life imagine one though), feel free to do so. All code snippets I post are just kickoff examples after all.
  • To all naive upvoters/commenters here: this is not a solution, but a workaround. The true underlying problem still stands and needs solving.
  • This fixed my situation. The solution would be for Java to start handling UTF-8 natively in resource bundles and in properties files. Until that happens I'll use a workaround.