I'm trying to load a proprieties from a text file, but the accented characters (saül) comes in a different encoding other than UTF-8 how to avoid it?
My property file have a property with an accented character (saül). How ever when I remote debug I find that properties.load(bufferedReader); takes that as saül so when I write to another file it gets written as saül, I have UTF-8 encoding everywhere else in the application. I'm not sure what I'm doing wrong while reading the properties from the file.
try {
final String propertyFilePath = System.getProperty(JVM_ARGUMENT_NAME);
if (StringUtils.hasText(propertyFilePath)) {
setLocalOverride(true);
resource = getApplicationContext().getResource(propertyFilePath);
BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(new FileInputStream(propertyFilePath), "UTF8"));
properties.load(bufferedReader);
externalFilePasswordConfigurer.afterPasswordPropertiesSet(properties);
LOGGER.info("ExternalFilePropertyConfigurer UTF-8 Reader");
}
setProperties(properties);
logProperties(properties);
} catch (Exception e) {
LOGGER.error("ExternalFilePropertyConfigurer setter failed to set properties: ", e);
throw new RuntimeException(e);
}
Old question, but as far as I know any .properties file must be in ISO-8859-1 charset or there'll be troubles.
When accented characters are required inside a properties file, each character must be replaced with its unicode version. In this particular case "saül" must be changed to "sa\u00FCl", where \u00FC is "ü".
Another solution is change the file type from .properties to .xml
See java documentation here:
The load(Reader) / store(Writer, String) methods load and store properties from and to a character based stream in a simple line-oriented format specified below. The load(InputStream) / store(OutputStream, String) methods work the same way as the load(Reader)/store(Writer, String) pair, except the input/output stream is encoded in ISO 8859-1 character encoding. Characters that cannot be directly represented in this encoding can be written using Unicode escapes as defined in section 3.3 of The Java™ Language Specification; only a single 'u' character is allowed in an escape sequence. The native2ascii tool can be used to convert property files to and from other character encodings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With