How to convert an UTF String to ANSI and Create an ANSI text file in SSD with JAVA-ANDROID

∥☆過路亽.° 提交于 2019-12-01 00:09:08

Strings are represented within the JVM as Unicode. When you write it out, you need to convert it to an appropriate character set.

To do this, use an OutputStreamWriter and specify the Charset.

OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file, true), 
                                                   "windows-1252");
writer.append(textBody);
writer.close();

Regarding your first question:

I think I know that to convert Strings to ANSI I should use:
String unicode = new String(asciiBytes, "windows-1252");
Is that correct?

Given an array of bytes representing characters in the 'windows-1252' character set, this code will construct you the appropriate Java String (internally represented as Unicode within the JVM). This isn't "converting Strings to ANSI" ... it's doing the opposite: converting 'windows-1252' to Unicode.

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