• <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>

            逛奔的蝸牛

            我不聰明,但我會很努力

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

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

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


            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 ) // 單獨出現(xiàn)BF以下的,也算是GBK   
            31.                     break;   
            32.                     if ( 0xC0 <= read && read <= 0xDF ) {   
            33.                         read = bis.read();   
            34.                         if ( 0x80 <= read && read <= 0xBF ) // 雙字節(jié) (0xC0 - 0xDF) (0x80   
            35.                                                                         // - 0xBF),也可能在GB編碼內(nèi)   
            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 逛奔的蝸牛 閱讀(4519) 評論(0)  編輯 收藏 引用 所屬分類: Java
            99久久无色码中文字幕| 国产99久久久国产精品~~牛| 久久精品成人免费观看97| 久久夜色tv网站| 亚洲国产精品成人久久蜜臀| 一本久久a久久精品亚洲| 久久精品黄AA片一区二区三区| 夜夜亚洲天天久久| 一级女性全黄久久生活片免费| 99久久精品免费看国产一区二区三区| 色欲久久久天天天综合网| 99久久无码一区人妻| 思思久久99热只有频精品66| 久久亚洲精品无码AV红樱桃| 久久影院久久香蕉国产线看观看| 亚洲va中文字幕无码久久| 久久996热精品xxxx| 久久久一本精品99久久精品88| 久久久精品久久久久特色影视| 国产午夜精品久久久久九九| 久久精品国产亚洲综合色 | 亚洲欧洲中文日韩久久AV乱码| 久久99国产亚洲高清观看首页| 1000部精品久久久久久久久| 久久夜色精品国产www| 久久只这里是精品66| 香蕉aa三级久久毛片| 久久久久人妻一区二区三区vr| 97精品国产91久久久久久| 国产精品美女久久久免费| 2021国产精品久久精品| 欧美亚洲另类久久综合| 一本久久综合亚洲鲁鲁五月天| 久久国产精品99精品国产| 大蕉久久伊人中文字幕| 久久人妻AV中文字幕| 国产精久久一区二区三区| 狠狠色丁香久久婷婷综合_中 | 久久99精品久久久久子伦| 久久亚洲高清综合| 久久99精品久久久久婷婷|