this is intended behavior. the propertyresourcebundle relies on the properties class, whose load method always assumes the file to be encoded is iso-latin-1¹:

the input stream is in a simple line-oriented format as specified in load(reader) and is assumed to use the iso 8859-1 character encoding; that is each byte is one latin1 character. characters not in latin1, and certain special characters, are represented in keys and elements using unicode escapes as defined in section 3.3 of the java™ language specification.

so converting your copied characters to unicode escape sequences in the right thing to ensure that they will be loaded properly. at runtime, the resourcebundle will contain the right character content.

while in eclipse, source files usually inherit the charset setting from their parent, to end up at the project or even system wide setting, it supports setting the charset encoding for single files and conveniently changes it automatically to iso-latin-1 for .properties files.

note that starting with java 9, you can use utf-8 for properties resource bundles. this does not require additional configuration actions, as the charset encoding is determined by probing. as the documentation of the propertyresourcebundle(inputstream) constructor states:

this constructor reads the property file in utf-8 by default. if a malformedinputexception or an unmappablecharacterexception occurs on reading the input stream, then the propertyresourcebundle instance resets to the state before the exception, re-reads the input stream in iso-8859-1 and continues reading. 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.

this works, as both encodings are identical for ascii characters, while for non-ascii sequences, it practically never happens for real life text that an iso-latin-1 sequence forms a valid utf-8 sequence. this applies to propertyresourcebundle which handles this probing, not for the properties class, which still only uses iso-latin-1 in its load(inputstream) method.

¹ i kept the statement in this absolute form for simplicity, despite, as elaborated at the end of this answer, java 9 has lifted this restriction.

Related Query

More Query from same tag