[英]org.apache.commons.io.FileUtils - change text encodig
I have one file which contains text(ukranian symbols). 我有一个包含文本(乌克兰符号)的文件。 Example:
${name} - привіт
. 示例:
${name} - привіт
。 I need to replace from ${name}
to Саша
. 我需要将
${name}
替换为Саша
。 But in output file I recive something like this: пÑ??Ð¸Ð²ÐµÑ - привіт
instead of Саша- привіт
. 但是在输出文件中,我会
пÑ??Ð¸Ð²ÐµÑ - привіт
类似以下内容: пÑ??Ð¸Ð²ÐµÑ - привіт
而不是Саша- привіт
。 But if I use .txt instead .rtf - all is fine. 但是,如果我使用.txt而不是.rtf-一切都很好。 I understand the problem in the coding but can't solve it.
我了解编码中的问题,但无法解决。
Code example: 代码示例:
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();
http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#readFileToString%28java.io.File%29 http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#readFileToString%28java.io.File%29
Deprecated.
已过时。 2.5 use readFileToString(File, Charset) instead
2.5使用readFileToString(File,Charset)代替
Also, make sure that the java compiler is reading your Java source with the same encoding as it is stored in (UTF-8 recommended), with the -encoding
switch. 此外,确保Java编译器与相同的编码读你的Java源,因为它是存储在(UTF-8推荐),用
-encoding
开关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.