判斷文件的編碼

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

  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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章