2

我检查了 jvm 中的默认文件编码:

System.out.println("***file.encoding::" + System.getProperty("file.encoding")); 
// ***file.encoding::Cp1252

但是当我使用 FileWriter 编写新文件时:

bf = new BufferedWriter(new FileWriter(file));

然后,我使用 cmd 重新检查编码文件:

file -i output-file.txt
output-file.txt: text/plain; charset=iso-8859-1

为什么字符集不是 Cp1252 而不是 iso-8859-1?

4

1 回答 1

1

cp1252 and iso-8859-1 are very similar encodings, and file might not be able to tell the difference based on the content of your file if both would be valid encodings.

Text files don't contain any metadata about the file encoding, to the only way to know is to read some bytes and guess. For byte values below 128 (ie, most normal English text) the two encoding are identical, so there is no way to tell which was used to write the file if those are the only characters in the file.

于 2018-05-03T17:18:21.770 回答