如何将一个UTF字符串转换为ANSI和创建在SSD ANSI文本文件,JAVA,机器人字符串、转换为、如何将、机器人

2023-09-06 04:24:23 作者:弦上春雪。

我在Java中写了一个Android应用程序获取用户的答案,并将其保存在一个文件中。 问题是,这个文件保存在UTF-8。 最终用户将在IBM SPSS打开这些文件,应用程序对于只能在ANSI(窗口1252)。

I wrote a android app in java to get user answers and save them in a file. The problem is that this file is saved in utf-8. The end user will open these files in the IBM SPSS, an application for windows that can read files only in ANSI (windows-1252).

如何创建的ANSI code文件保存在SSD卡从Java的Andr​​oid应用程序?

我想我知道,字符串转换为ANSI我应该使用:

I think I know that to convert Strings to ANSI I should use:

String unicode = new String(asciiBytes, "windows-1252");

是这样吗?

我的code保存该文件是这样的:

My code to save the file is this:

File interviewFile = new File(placeOfSDD, fileName);
FileWriter writer = new FileWriter(interviewFile, true);
writer.append(textBody);
writer.flush();
writer.close();

的TextBody是要转换的字符串为ANSI,而interviewFile要保存为ANSI文件太

"textBody" is the String to convert to ANSI, and the "interviewFile" is the file to be saved as ANSI too.

感谢您的帮助!

推荐答案

字符串被重新$ P $。当你写出来,你需要将其转换为相应的字符集。

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

要做到这一点,使用 OutputStreamWriter 并指定字符集

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:

我想我知道,字符串转换为ANSI我应该使用:   字符串UNI code =新的String(asciiBytes,窗口-1252);   这是否正确?

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

由于字节数组再在'窗口1252'字符集presenting字符,这code将建设你相应的Java的String(内部重新psented作为内统一code $ P $在JVM)。这的不是的将字符串转换为ANSI......它在做相反的:将视窗1252,以统一code

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.