判定文件是GBK还是UTF-8编码 @param f @return
(File f)
| 19 | * @return |
| 20 | */ |
| 21 | public static String detection(File f){ |
| 22 | String charset = "GBK"; |
| 23 | byte[] first3Bytes = new byte[3]; |
| 24 | try { |
| 25 | boolean checked = false; |
| 26 | BufferedInputStream bis = new BufferedInputStream( new FileInputStream( f ) ); |
| 27 | bis.mark( 0 ); |
| 28 | int read = bis.read( first3Bytes, 0, 3 ); |
| 29 | if ( read == -1 ) return charset; |
| 30 | if ( first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE ) { |
| 31 | charset = "UTF-16LE"; |
| 32 | checked = true; |
| 33 | } |
| 34 | else if ( first3Bytes[0] == (byte) 0xFE && first3Bytes[1] == (byte) 0xFF ) { |
| 35 | charset = "UTF-16BE"; |
| 36 | checked = true; |
| 37 | } |
| 38 | else if ( first3Bytes[0] == (byte) 0xEF && first3Bytes[1] == (byte) 0xBB && first3Bytes[2] == (byte) 0xBF ) { |
| 39 | charset = "UTF-8"; |
| 40 | checked = true; |
| 41 | } |
| 42 | bis.reset(); |
| 43 | if ( !checked ) { |
| 44 | // int len = 0; |
| 45 | int loc = 0; |
| 46 | |
| 47 | while ( (read = bis.read()) != -1 ) { |
| 48 | loc++; |
| 49 | if ( read >= 0xF0 ) break; |
| 50 | if ( 0x80 <= read && read <= 0xBF ) // 单独出现BF以下的,也算是GBK |
| 51 | break; |
| 52 | if ( 0xC0 <= read && read <= 0xDF ) { |
| 53 | read = bis.read(); |
| 54 | if ( 0x80 <= read && read <= 0xBF ) // 双字节 (0xC0 - 0xDF) (0x80 |
| 55 | // - 0xBF),也可能在GB编码内 |
| 56 | continue; |
| 57 | else break; |
| 58 | } |
| 59 | else if ( 0xE0 <= read && read <= 0xEF ) {// 也有可能出错,但是几率较小 |
| 60 | read = bis.read(); |
| 61 | if ( 0x80 <= read && read <= 0xBF ) { |
| 62 | read = bis.read(); |
| 63 | if ( 0x80 <= read && read <= 0xBF ) { |
| 64 | charset = "UTF-8"; |
| 65 | break; |
| 66 | } |
| 67 | else break; |
| 68 | } |
| 69 | else break; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | bis.close(); |
| 75 | } catch ( Exception e ) { |
| 76 | //e.printStackTrace(); |
| 77 | if (logger.isErrorEnabled()) { |
| 78 | logger.error("判定文件是GBK还是UTF-8编码",e); |