Reputation: 175
I am trying to read in .properties files with many different languages, add new entries to them, sort, and print back to file. I have the encoding as UTF-8, and it works for all my current languages except Russian. When reading the file in I get all question marks from the Russian file. When it prints back out it has a lot of the correct text, but has random question marks here and there. Here is my code for reading in the file.
Properties translation = new Properties() {
private static final long serialVersionUID = 1L;
@Override
public synchronized Enumeration<Object> keys() {
return Collections.enumeration(new TreeSet<Object>(super.keySet()));
}
}
byte[] readIn = Files.readAllBytes(Paths.get(filePath));
String replacer = new String(readIn).replace("\\","\\\\");
translation.load(new InputStreamReader(new ByteArrayInputStream(replacer.getBytes()),"UTF-8"));
Upvotes: 1
Views: 1773
Reputation: 974
Sometimes changing the encoding to utf-8 gives rise to errors, such as some extra characters or does nothing. The link: How can i read a Russian file in Java? may help you.
Upvotes: 0
Reputation: 691645
new String(readIn)
and replacer.getBytes()
don't use UTF8. They use your platform default encoding. Pass StandardCharsets.UTF_8
as an additional argument to both calls.
BTW, transforming a STring to a byte array, to then transform back the bytes to characters and reading them is a waste of time and resources. Just do
translation.load(new StringReader(replacer));
Upvotes: 2