【发布时间】:2018-10-25 00:43:32
【问题描述】:
我在将以下字符串正确写入文件时遇到问题。尤其是“-”字。问题出现在我的本地计算机 (Windows 7) 和服务器 (Linux) 上
字符串:“Cœurs d'artichauts Grillées”
-
有效 (œ gets displays correctly, while the apostrophe get translated into a question mark):
Files.write(path, content.getBytes(StandardCharsets.ISO_8859_1)); -
不起作用(result in file):
Files.write(path, content.getBytes(StandardCharsets.UTF_8));
根据this question 的第一个答案,UTF-8 应该也能够正确编码–。有谁知道我做错了什么?
【问题讨论】:
-
如果第一种方法“有效”,您似乎不需要 UTF-8。因为那个写的是 ISO-8859-1。你确定你用来显示输出的东西真的需要 UTF-8 吗?
-
您的字符串
content也可能已经损坏。如果是字符串文字,您的 Java 源文件编码是什么?它必须与您的编辑认为的相符。 -
@Thilo 发现问题。绳子提前断了。断弦:“Curs d'artichauts Grillées” 应该是什么样子:“Cœurs d'artichauts Grillées” 编辑:这里也没有显示断弦。
标签: java encoding utf-8 character-encoding iso-8859-1