简体   繁体   中英

Default java file.encoding is Cp1252 but created file is ISO-8859-1

I checked the default file encoding in jvm:

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

But when I wrote new file using FileWriter:

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

then, I recheck encoding file by using cmd:

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

why charset isn't Cp1252 instead of iso-8859-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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM