徹底解決android讀取中文txt的亂碼(自動判斷文件類型並轉碼

來源:互聯網
上載者:User

標籤:android   blog   http   io   os   ar   sp   art   on   

原文:http://blog.csdn.net/handsomedylan/article/details/6138400

public String convertCodeAndGetText(String str_filepath) {// 轉碼

                File file = new File(str_filepath);
                BufferedReader reader;
                String text = "";
                try {
                        // FileReader f_reader = new FileReader(file);
                        // BufferedReader reader = new BufferedReader(f_reader);
                        FileInputStream fis = new FileInputStream(file);
                        BufferedInputStream in = new BufferedInputStream(fis);
                        in.mark(4);
                        byte[] first3bytes = new byte[3];
                        in.read(first3bytes);//找到文檔的前三個位元組並自動判斷文件類型。
                        in.reset();
                        if (first3bytes[0] == (byte) 0xEF && first3bytes[1] == (byte) 0xBB
                                        && first3bytes[2] == (byte) 0xBF) {// utf-8

                                reader = new BufferedReader(new InputStreamReader(in, "utf-8"));

                        } else if (first3bytes[0] == (byte) 0xFF
                                        && first3bytes[1] == (byte) 0xFE) {

                                reader = new BufferedReader(
                                                new InputStreamReader(in, "unicode"));
                        } else if (first3bytes[0] == (byte) 0xFE
                                        && first3bytes[1] == (byte) 0xFF) {

                                reader = new BufferedReader(new InputStreamReader(in,
                                                "utf-16be"));
                        } else if (first3bytes[0] == (byte) 0xFF
                                        && first3bytes[1] == (byte) 0xFF) {

                                reader = new BufferedReader(new InputStreamReader(in,
                                                "utf-16le"));
                        } else {

                                reader = new BufferedReader(new InputStreamReader(in, "GBK"));
                        }
                        String str = reader.readLine();

                        while (str != null) {
                                text = text + str + "/n";
                                str = reader.readLine();

                        }
                        reader.close();

                } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
                return text;
        }
代碼不難,覺得有用的可以頂一下。

徹底解決android讀取中文txt的亂碼(自動判斷文件類型並轉碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.