简体   繁体   中英

Why is text file encoding still ANSI when in the java code i am specifying it be UTF-8

I am writing a some text into file using the FileWriter object. I am specifying that I want the output to be in UTF-8,but when i open the text file and go to save as , I see that it is in ANSI encoding.

I want to also add that when there are characters other than the standard ascii charset (eg:- japansese character) the file encoding is UTF-8, but without then text file encoding is ANSI.

File json_file= new File(path);
FileWriter json_file_output=newFileWriter(json_file,StandardCharsets.UTF_8);
json_file_output.write("SOME JSON TEXT HERE");
json_file_output.flush();

I am not sure whether it is due to java code or notepad.

Thank you for the help.

There is no such thing as ANSI encoding . See What is ANSI format? .

Likely what is meant is US-ASCII . And every 8-bit US-ASCII file is also a UTF-8 file . Unicode is a superset of US-ASCII. When written out using octets, ASCII files are UTF-8 files. UTF-8 encoding was designed this way on purpose, to be compatible.

US-ASCII is a 7-bit character set, having only 128 characters, numbered 0-127. So if written using octets (8-bits), the first bit of every octet is a zero. See the Wikipedia page on UTF-8 encoding , and notice the role played by the first bit.

Your text editor is likely looking at the domain of characters found in your file, and then trying to be helpfully conservative in labeling the file using the smallest-scope encoding possible. If only US-ASCII characters, then label as US-ASCII (and apparently misreport as “ANSI”). As soon as you add higher-numbered characters with a code point beyond that of ASCII, then label as UTF-8.

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