• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            逛奔的蝸牛

            我不聰明,但我會很努力

               ::  :: 新隨筆 ::  ::  :: 管理 ::
              首先,不同編碼的文本,是根據文本的前兩個字節來定義其編碼格式的。定義如下:

              ANSI:        無格式定義;
              Unicode:       前兩個字節為FFFE;
              Unicode big endian: 前兩字節為FEFF;  
              UTF-8:        前兩字節為EFBB; 

              知道了各種編碼格式的區別,寫代碼就容易了.


            1. public static String get_charset( File file ) {   
            2.         String charset = "GBK";   
            3.         byte[] first3Bytes = new byte[3];   
            4.         try {   
            5.             boolean;   
            6.             BufferedInputStream bis = new BufferedInputStream( new FileInputStream( file ) );   
            7.             bis.mark( 0 );   
            8.             int read = bis.read( first3Bytes, 03 );   
            9.             if ( read == -1 ) return charset;   
            10.             if ( first3Bytes[0] == (byte0xFF && first3Bytes[1] == (byte0xFE ) {   
            11.                 charset = "UTF-16LE";   
            12.                 checked = true;   
            13.             }   
            14.             else if ( first3Bytes[0] == (byte0xFE && first3Bytes[1] == (byte0xFF ) {   
            15.                 charset = "UTF-16BE";   
            16.                 checked = true;   
            17.             }   
            18.             else if ( first3Bytes[0] == (byte0xEF && first3Bytes[1] == (byte0xBB && first3Bytes[2] == (byte0xBF ) {   
            19.                 charset = "UTF-8";   
            20.                 checked = true;   
            21.             }   
            22.             bis.reset();   
            23.             if ( !checked ) {   
            24.             //    int len = 0;   
            25.                 int loc = 0;   
            26.   
            27.                 while ( (read = bis.read()) != -1 ) {   
            28.                     loc++;   
            29.                     if ( read >= 0xF0 ) break;   
            30.                     if ( 0x80 <= read && read <= 0xBF ) // 單獨出現BF以下的,也算是GBK   
            31.                     break;   
            32.                     if ( 0xC0 <= read && read <= 0xDF ) {   
            33.                         read = bis.read();   
            34.                         if ( 0x80 <= read && read <= 0xBF ) // 雙字節 (0xC0 - 0xDF) (0x80   
            35.                                                                         // - 0xBF),也可能在GB編碼內   
            36.                         continue;   
            37.                         else break;   
            38.                     }   
            39.                     else if ( 0xE0 <= read && read <= 0xEF ) {// 也有可能出錯,但是幾率較小   
            40.                         read = bis.read();   
            41.                         if ( 0x80 <= read && read <= 0xBF ) {   
            42.                             read = bis.read();   
            43.                             if ( 0x80 <= read && read <= 0xBF ) {   
            44.                                 charset = "UTF-8";   
            45.                                 break;   
            46.                             }   
            47.                             else break;   
            48.                         }   
            49.                         else break;   
            50.                     }   
            51.                 }   
            52.                 //System.out.println( loc + " " + Integer.toHexString( read ) );   
            53.             }   
            54.   
            55.             bis.close();   
            56.         } catch ( Exception e ) {   
            57.             e.printStackTrace();   
            58.         }   
            59.   
            60.         return charset;   
            61.     }   
            From: http://ajava.org/code/I18N/14816.html
            posted on 2009-11-04 15:03 逛奔的蝸牛 閱讀(4512) 評論(0)  編輯 收藏 引用 所屬分類: Java
            国产欧美久久一区二区| 九九热久久免费视频| 伊人久久大香线蕉成人| 2021国内久久精品| 久久精品a亚洲国产v高清不卡| .精品久久久麻豆国产精品| 精品无码久久久久久久动漫| 国产69精品久久久久观看软件| 久久AV高清无码| 久久性精品| 免费精品久久天干天干| 国产精品99久久99久久久| 国产成人精品久久亚洲| 精产国品久久一二三产区区别| 久久亚洲国产中v天仙www| 色综合久久夜色精品国产| 久久综合久久综合久久综合| 2021久久精品免费观看| 欧美日韩精品久久久免费观看| 久久99国内精品自在现线| 一级做a爰片久久毛片毛片| 国产成人精品久久综合| 久久超碰97人人做人人爱| 久久久久久久精品妇女99| 久久久久亚洲AV无码专区网站| 久久综合九色综合欧美狠狠| 久久久久人妻一区二区三区vr | 国产成人综合久久综合| 伊人色综合九久久天天蜜桃| 精品久久久久久国产三级 | 国产精品综合久久第一页| 麻豆一区二区99久久久久| 久久国语露脸国产精品电影| 欧美精品丝袜久久久中文字幕| 99久久无码一区人妻| 国产福利电影一区二区三区久久久久成人精品综合 | 久久亚洲精品成人无码网站| 日韩一区二区三区视频久久| 亚洲性久久久影院| 久久亚洲sm情趣捆绑调教| 国产亚洲美女精品久久久2020|