1. 程式人生 > >Android 匯出CSV檔案,解決中文亂碼問題

Android 匯出CSV檔案,解決中文亂碼問題

亂碼,無非就是編碼對不上的問題,只要編碼一樣那就沒問題,使用了Okio,直接上程式碼:

            try {
                    //寫入檔案
                    String data = stringBuilder.toString();
                    File file = new File(getExportFilePath() + getExportFileName());
                    Okio.buffer(Okio.sink(file)).write(new byte[]{(byte
) 0xEF, (byte) 0xBB, (byte) 0xBF}).writeUtf8(data).flush(); } catch (IOException e) { e.printStackTrace(); }

關鍵點在於,寫入資料前,先在開頭寫表明當前檔案的編碼格式為UTF-8:new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}

參考: