Search code examples
javaandroidfileencodingutf-8

How to read an ansi-coded file with german (european) characters and display correctly


I have a standard textfile written with Windows editor (or Word) und saved in ANSI-format in the android device. If I open and read this file and display it on my Android device all characters are displayed correctly except the German Umlaute äÄöÖüÜß. Instead of these characters a white question mark is shown within a black diamond. (I display them in a homescreen widget using remoteViews.setTextViewText(...)

I googled for hours and found lots of hints on using UTF-8 encoding etc. But when I save the file in UTF-8 or with any format but ANSI, I will get an exception and can't read the file at all. Using an android editor shows the encoding of the file is correct both in ANSI and in UTF-8.

My program is too long to copy it here so I extracted the hopefully relevant part and put it below. Please help!

public class Test {

    static void readFile() {
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "birthday.txt");
        if (file.exists())
        {
            try {
                FileReader fileReader = new FileReader(file);
                BufferedReader br = new BufferedReader(fileReader);
                //BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(directory+"birthday.txt"), "windows-1252"));

                String lineOfText;
                while ((lineOfText = br.readLine()) != null) {
                    //Output lineOfText  via remoteViews.setTextViewText(WidgetOutput.getRef(linecounter).getIdWhat(), lineOfText);

                }
                br.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

Solution

  • The commented BufferedReader line looks like it should work. However, windows-1252 is the canonical name for classes in java.nio. For classes in java.io (like InputStreamReader) the canonical name is Cp1252. See Supported Encodings

    You may also wish to try ISO-8859-1 (nio) or ISO8859_1 (io).