4

I get different results on two different systems and don't know why.

Properties prop = new Properties();
prop.load(new ByteArrayInputStream(input)); //input is byte[]

On both systems input contains "var=\\u00C4\\u00DC\\u00D6\\u00E4\\u00FC\\u00F6".

On my test system prop contains "var=ÄÜÖäüö". (This is what I want)

On another system prop contains "var=\xC4\xDC\xD6\xE4\xFC\xF6". This is input in hex, but why does Properties do this? I unfortunately know nothing about the other systems configuration.

Has someone an idea about the reason?

Tobi Nary
  • 4,566
  • 4
  • 30
  • 50
unknown
  • 51
  • 1
  • 2

1 Answers1

8

Java .properties files are encoded with ISO-8859-1 (Latin-1), NOT UTF-8. All non-Latin-1 characters must be entered by using Unicode escape characters, e.g. \uHHHH.

An alternative is to use the XML format for properties, which IS UTF-8.

Source: Javadoc

Also see this SO question

And this one

Community
  • 1
  • 1
Stewart
  • 17,616
  • 8
  • 52
  • 80
  • Thank you Stewart for your answer. I know that my byte[] input must be ISO-8859-1. I use for example "\\u00C4" insteed of "Ä". But prob.toString() should give me the encoded value ("var=ÄÜÖäüö"). But on a foreign system it is "var=\xC4\xDC\xD6\xE4\xFC\xF6". I don't know why. – unknown Jan 11 '16 at 10:54
  • 1
    To complete the answer, we can note that this will be changed with Java 9. See http://openjdk.java.net/jeps/226 – Prim Jan 11 '16 at 10:57
  • @unknown I think you're going to have to tell us something about what is different between the systems. C4 is certainly the correct code. http://www.htmlhelp.com/reference/html40/entities/latin1.html – Stewart Jan 11 '16 at 12:49
  • @Prim - Well, that's not going to cause headaches for a million developers across the planet, will it? Many, especially in the corporate world, are still on Java 7, or even 6. – Stewart Jan 11 '16 at 12:50
  • 1
    @Prim: That's only for PropertyResourceBundle, not for properties files in general; and even PropertyResourceBundle will fall back to ISO-8859-1. – nmatt Apr 26 '21 at 16:29