Hi all,

I encountered the problem that the BufferedReader causes garbled
characters when I read UTF-8 file.
I confirmed that this problem occurred on both Emulator(1.5) and ADP1
(1.5).

I posted same message into Japanese community.
<http://groups.google.com/group/android-group-japan/browse_thread/
thread/8cdb21ac8e1210fc>
They said this problem didn't occur on Linux JavaVM (based on UTF-8)
with almost the same code as this, but occurred on Android Emulator.

Does anyone know workaround?


----------------------------------------
Test code:
----------------------------------------
public class TestBufferedReader extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //vvvvvvvvvv

        // Make text file in Japanese
        String strFullPathName = "/data/data/" + getPackageName() + "/
test.txt";
        makeFile(strFullPathName);

        // Test reading and writing text file
        testReadWrite(strFullPathName);

        //^^^^^^^^^^
    }


    //vvvvvvvvvv

    // Make text file in Japanease
    public void makeFile(String strFullPathName) {
        File fileOut = new File(strFullPathName);
        fileOut.getParentFile().mkdirs();

        FileWriter fw = null;
        BufferedWriter bw = null;

        try {
            fw = new FileWriter(fileOut);
            bw = new BufferedWriter(fw);

            String str = "あいうえおかきくけこ";        //
"abcdefghijklmnopqrstuvwxyz"; is OK. but Japanese/Chinese characters
cause problem.
            for (int i = 0; i < 4000; ++i) {
                bw.write(str);
                bw.newLine();
            }
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
        }
        finally {
            if (bw != null) {
                try {
                    bw.close();
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // Test reading and writing text file
    public void testReadWrite(String strFullPathName) {

        // input
        File fileIn = new File(strFullPathName);
        FileReader fr = null;
        BufferedReader br = null;

        // output
        File fileOut = new File(strFullPathName + ".tmp");
        FileWriter fw = null;
        BufferedWriter bw = null;

        try {
            fr = new FileReader(fileIn);
            br = new BufferedReader(fr);    // If the buffer size is
specified, frequency of the problem falls.

            fw = new FileWriter(fileOut);
            bw = new BufferedWriter(fw);

            int nLine = 0;
            String str;
            while ((str = br.readLine()) != null) {
                ++nLine;
                if (str.indexOf('\uFFFD') >= 0) {   // If the garble
happens, it is converted into '\uFFFD'.
                    Log.e("test", "read error! at line " +
Integer.toString(nLine));
                }

                bw.write(str);
                bw.newLine();
            }
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
        }
        finally {
            if (br != null) {
                try {
                    br.close();
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (bw != null) {
                try {
                    bw.close();
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //^^^^^^^^^^
}
----------------------------------------


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to