判断文件的编码

首先,不同编码的文本,是根据文本的前两个字节来定义其编码格式的。定义如下:

  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 @ 2010-01-02 16:45  张兴业  阅读(359)  评论(0编辑  收藏  举报