激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

香港云服务器
服務(wù)器之家 - 編程語言 - JAVA教程 - java讀取文件字符集示例方法

java讀取文件字符集示例方法

2019-11-08 14:30java技術(shù)網(wǎng) JAVA教程

這篇文章主要介紹了java讀取文件字符集的示例,需要的朋友可以參考下

代碼如下:

public static String getCharset(File file) {
        String charset = "GBK";
        byte[] first3Bytes = new byte[3];
        try {
            boolean checked = false;
            BufferedInputStream bis = new BufferedInputStream(
                  new FileInputStream(file));
            bis.mark(0);
            int read = bis.read(first3Bytes, 0, 3);
            if (read == -1)
                return charset;
            if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) {
                charset = "UTF-16LE";
                checked = true;
            } else if (first3Bytes[0] == (byte) 0xFE && first3Bytes[1]
                == (byte) 0xFF) {
                charset = "UTF-16BE";
                checked = true;
            } else if (first3Bytes[0] == (byte) 0xEF && first3Bytes[1]
                    == (byte) 0xBB
                    && first3Bytes[2] == (byte) 0xBF) {
                charset = "UTF-8";
                checked = true;
            }
            bis.reset();
            if (!checked) {
                int loc = 0;
                while ((read = bis.read()) != -1) {
                    loc++;
                    if (read >= 0xF0)
                        break;
                    //單獨(dú)出現(xiàn)BF以下的,也算是GBK
                    if (0x80 <= read && read <= 0xBF)
                        break;
                    if (0xC0 <= read && read <= 0xDF) {
                        read = bis.read();
                        if (0x80 <= read && read <= 0xBF)// 雙字節(jié) (0xC0 - 0xDF)
                            // (0x80 -
                            // 0xBF),也可能在GB編碼內(nèi)
                            continue;
                        else
                            break;
                     // 也有可能出錯(cuò),但是幾率較小
                    } else if (0xE0 <= read && read <= 0xEF) {
                        read = bis.read();
                        if (0x80 <= read && read <= 0xBF) {
                            read = bis.read();
                            if (0x80 <= read && read <= 0xBF) {
                                charset = "UTF-8";
                                break;
                            } else
                                break;
                        } else
                            break;
                    }
                }
                System.out.println(loc + " " + Integer.toHexString(read));
            }
            bis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return charset;
    }

延伸 · 閱讀

精彩推薦
485
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25
主站蜘蛛池模板: 国产69精品久久久久99尤 | 日韩欧美中文字幕视频 | 精品日韩欧美 | 国产精品久久久久久影院8一贰佰 | 爽妇网国产精品 | 成人在线视频免费看 | japanesexxxxxxxhd| 欧美成人高清视频 | 久久亚洲精品久久国产一区二区 | 澳门一级淫片免费视频 | 成人男女视频 | 欧日一级片| 国产一级一级片 | 成人国产精品一区二区毛片在线 | 成人国产精品一区 | 亚洲精品 欧美 | 久久久久久久久国产精品 | 成人富二代短视频 | 国产精品一区在线观看 | 国产在线1区 | 亚洲嫩草av | 久久国产免费视频 | 色诱亚洲精品久久久久久 | 亚洲欧美日韩久久精品第一区 | 欧美成人精品一区二区 | 91精品国产一区二区三区四区在线 | 精品一区二区在线播放 | 亚洲成人中文字幕在线 | 欧美黄色一级片视频 | 免费观看国产精品视频 | 激情免费视频 | 久久久久久久久成人 | 国产pron| 国产91porn | 久久久久久久一区二区 | 一级做a爱片毛片免费 | 国产1区2区3区中文字幕 | 91看片王| 一级免费a| 亚洲性生活免费视频 | 一级免费毛片 |