Grumbunks
Grumbunks

Reputation: 1267

Using java.util.Properties to read accented strings from properties file

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.

Upvotes: 0

Views: 2318

Answers (2)

s_u_f
s_u_f

Reputation: 200

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")));

Upvotes: 1

Kayaman
Kayaman

Reputation: 73528

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

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

Upvotes: 1

Related Questions