简体   繁体   English

org.apache.commons.io.FileUtils-更改文本编码

[英]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.

相关问题 如何在Servlet中改善org.apache.commons.io.FileUtils copyInputStreamToFile - How to improve org.apache.commons.io.FileUtils copyInputStreamToFile in Servlet 引起原因:java.lang.ClassNotFoundException:org.apache.commons.io.FileUtils,maven pom.xml wa - Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.FileUtils, maven pom.xml wa 我有导入org.apache.commons.io.FileUtils; 在我的代码中,它适用于eclipse但不适用于命令行 - I have import org.apache.commons.io.FileUtils; in my code and it works on eclipse but not in command line Java库,例如org.apache.commons.io.FileUtils,但包含实例方法而不是静态方法 - Java library like org.apache.commons.io.FileUtils but contains instance methods instead of static methods 使用org.apache.commons.io.FileUtils时Android内存泄漏 - Android memory leak when using org.apache.commons.io.FileUtils 使用Netbeans 8.0.2,并希望使用org.apache.commons.io.FileUtils; 怎么样? - Using Netbeans 8.0.2 and want to use org.apache.commons.io.FileUtils; how? org / apache / commons / io / FileUtils-NoClassDefFoundError - org/apache/commons/io/FileUtils - NoClassDefFoundError org.apache.commons.io.FileUtils.readFileToString 和文件路径中的空白 - org.apache.commons.io.FileUtils.readFileToString and blanks in filepath 在 org.apache.commons.io.FileUtils.copyURLToFile 上设置超时? - Set timeout on org.apache.commons.io.FileUtils.copyURLToFile? 缺少方法 org.apache.commons.io.FileUtils.forceMkdirParent? - missing methode org.apache.commons.io.FileUtils.forceMkdirParent?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM