Java: can't save in UTF-8

混江龙づ霸主 提交于 2019-12-04 06:19:19

问题


I have this line of code in java:

new BufferedWriter(new OutputStreamWriter(new FileOutputStream(name, append), "UTF-8"));

This writer does not write an UTF-8 file, because when I open it in notepad++ it says that the encoding is: ANSI as UTF-8. I need it to be pure UTF-8.

Do you have any suggestions?


回答1:


notepad++ (and any other tool) can only guess the encoding, it's not written anywhere in your file (or in some metadata).

And if the text you've written doesn't contain any characters outside the ASCII range (i.e. no character with a Unicode codepoint > 127), then a file with ANSI encoding is indistinguishable from one in UTF-8 encoding.




回答2:


Notepad++ uses a heuristic algorithm to detect the encoding, i.e. the detected encoding can differ from the true on (it's a guess).

In this case, Notepad++ is correct, but misunderlabeling the encoding. ANSI as UTF-8 is pure UTF-8, just without a BOM.




回答3:


Most likely Notepad++ needs the BOM at the beginning of your file. Write the bytes EF BB BF first to your file, then the encoded characters.



来源:https://stackoverflow.com/questions/6787645/java-cant-save-in-utf-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!