我有一个包含文本(乌克兰符号)的文件。示例:${name} - привіт
。我需要替换 from${name}
到Саша
。但在输出文件中,我收到这样的内容:пÑ??Ð¸Ð²ÐµÑ - привіт
而不是Саша- привіт
. 但是如果我使用 .txt 而不是 .rtf - 一切都很好。我理解编码中的问题,但无法解决。
代码示例:
File file = new File("original.rtf");
String content = FileUtils.readFileToString(file);
content = content.replace("${name}", "Саша");
File fileNew = new File("changed.rtf");
FileOutputStream fop = new FileOutputStream(fileNew);
byte[] contentInBytes = content.getBytes("UTF-8");
fop.write(contentInBytes);
fop.flush();
fop.close();