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?

2
  • You should try to display the ASCII codes. My guess is that you have the same values on both systems but fail at rendering it because of the platform's default encoding. Commented Jan 11, 2016 at 10:05
  • How are you displaying the values? Is it different on each system? Commented Jan 11, 2016 at 10:08

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

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.
To complete the answer, we can note that this will be changed with Java 9. See openjdk.java.net/jeps/226
@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. htmlhelp.com/reference/html40/entities/latin1.html
@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.
@Prim: That's only for PropertyResourceBundle, not for properties files in general; and even PropertyResourceBundle will fall back to ISO-8859-1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.