0

I'm trying to read properties from a constants.properties file using java.utils.Properties. Only, some of these properties contain accented characters like é, è, ô, and when I read them using getProperty(), the accents are removed. ie:

Générateur de formulaire

becomes

Generateur de formulaire

I know that Property files are read with the ISO 8859-1 encoding, so I've already tried switching out the characters for unicode escapes:

FORM_GENERATOR_VALUE=Générateur de formulaires

became

FORM_GENERATOR_VALUE=G\u0065n\u0065rateur de formulaires

However this still gives off the same result. When I halt execution and look at the variables, the strings I read from my file with getProperty() still have no accents.

Here is how my Properties are initialized:

public Properties constants = new Properties(); constants.load(new FileInputStream("constants.properties")); 

I've seen that one solution would be to switch the Property file format from .property to .xml but ideally I would like not to have to do that since it would imply rewriting my entire constant file again.

1
  • 1
    If you can determine with certainty what encoding your constants.properties file is using, you can specify an explicit encoding with a new InputStreamReader, and you can pass that InputStreamReader to load(Reader). Commented Oct 30, 2019 at 11:45

2 Answers 2

2

I had accent issue while using french characters so i used the below snippet. We need to pass the charset while reading the .properties file.

this.properties.load(new InputStreamReader( getClass().getClassLoader().getResourceAsStream("configuration.properties"), Charset.forName("UTF-8"))); 
Sign up to request clarification or add additional context in comments.

1 Comment

It is much better to use proper unicode escapes than specifying non standard encoding. You never know if a tool can change the encoding (IDE, version control...)
1

\u0065 is e, not é (which is \u00E9).

So once you set the correct unicode escape, it will be read properly.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.