How to Convert UTF String to ANSI and Create ANSI Text File on an SSD?

Asked

Viewed 2,848 times

4

I wrote an Android application using Java, so that users answer questions, which are then saved in a file. The problem is that this file is saved in UTF8. The end user will open this file on IBM SPSS which is an application for Windows and only reads files in ANSI (Windows-1252).

How I create files in ANSI to save on memory card in my Java-Android application?

To convert Strings to ANSI I must use:

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

That is correct?

My old code for saving the file is:

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

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

Already to read a file on ANSI and convert it to String with UTF8, how do I do?

  • 1

    Note that the conversion of UTF8->ANSI has loss of information. There are representable characters in UTF8 that are not in ANSI. ANSI->UTF8 conversion is safe.

1 answer

2

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.