![](/img/trans.png)
[英]How to improve org.apache.commons.io.FileUtils copyInputStreamToFile in Servlet
[英]org.apache.commons.io.FileUtils - change text encodig
我有一個包含文本(烏克蘭符號)的文件。 示例: ${name} - привіт
。 我需要將${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();
已過時。 2.5使用readFileToString(File,Charset)代替
此外,確保Java編譯器與相同的編碼讀你的Java源,因為它是存儲在(UTF-8推薦),用-encoding
開關。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.