Trang Nguyen
Trang Nguyen

Reputation: 21

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?

Upvotes: 2

Views: 719

Answers (1)

puhlen
puhlen

Reputation: 8529

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.

Upvotes: 1

Related Questions