上篇文章需要读取当前java或者配置文件的编码格式,这里主要支持UTF-8、GBK、UTF-16、Unicode等
1/** 2 * 判断文件的编码格式 3 * @param fileName :file 4 * @return 文件编码格式 5 * @throws Exception 6 */ 7 public static String codeString(File fileName) throws Exception{ 8 BufferedInputStream bin = new BufferedInputStream( 9 new FileInputStream(fileName)); 10 int p = (bin.read() << 8) + bin.read(); 11 String code = null; 12 13 switch (p) { 14 case 0xefbb: 15 code = "UTF-8"; 16 break; 17 case 0xfffe: 18 code = "Unicode"; 19 break; 20 case 0xfeff: 21 code = "UTF-16BE"; 22 break; 23 default: 24 code = "GBK"; 25 } 26 IOUtils.closeQuietly(bin); 27 return code; 28 }
上面这段代码只能判断带bom的文本,如果非bom文本,还有两种方式 1、 轮询常用的编码,知道找到匹配的,如下面一段测试代码
1/* 2 * Copyright 2010 Georgios Migdos <cyberpython@gmail.com>. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * under the License. 16 */ 17 18import java.io.BufferedInputStream; 19import java.io.File; 20import java.io.FileInputStream; 21import java.io.FileNotFoundException; 22import java.io.IOException; 23import java.io.InputStreamReader; 24import java.nio.ByteBuffer; 25import java.nio.charset.CharacterCodingException; 26import java.nio.charset.Charset; 27import java.nio.charset.CharsetDecoder; 28 29/** 30 * 31 * @author Georgios Migdos <cyberpython@gmail.com> 32 */ 33public class CharsetDetector { 34 35 public Charset detectCharset(File f, String[] charsets) { 36 37 Charset charset = null; 38 39 for (String charsetName : charsets) { 40 charset = detectCharset(f, Charset.forName(charsetName)); 41 if (charset != null) { 42 break; 43 } 44 } 45 46 return charset; 47 } 48 49 private Charset detectCharset(File f, Charset charset) { 50 try { 51 BufferedInputStream input = new BufferedInputStream(new FileInputStream(f)); 52 53 CharsetDecoder decoder = charset.newDecoder(); 54 decoder.reset(); 55 56 byte[] buffer = new byte[512]; 57 boolean identified = false; 58 while ((input.read(buffer) != -1) && (!identified)) { 59 identified = identify(buffer, decoder); 60 } 61 62 input.close(); 63 64 if (identified) { 65 return charset; 66 } else { 67 return null; 68 } 69 70 } catch (Exception e) { 71 return null; 72 } 73 } 74 75 private boolean identify(byte[] bytes, CharsetDecoder decoder) { 76 try { 77 decoder.decode(ByteBuffer.wrap(bytes)); 78 } catch (CharacterCodingException e) { 79 return false; 80 } 81 return true; 82 } 83 84 public static void main(String[] args) { 85 File f = new File("example.txt"); 86 87 String[] charsetsToBeTested = {"UTF-8", "windows-1253", "ISO-8859-7"}; 88 89 CharsetDetector cd = new CharsetDetector(); 90 Charset charset = cd.detectCharset(f, charsetsToBeTested); 91 92 if (charset != null) { 93 try { 94 InputStreamReader reader = new InputStreamReader(new FileInputStream(f), charset); 95 int c = 0; 96 while ((c = reader.read()) != -1) { 97 System.out.print((char)c); 98 } 99 reader.close(); 100 } catch (FileNotFoundException fnfe) { 101 fnfe.printStackTrace(); 102 }catch(IOException ioe){ 103 ioe.printStackTrace(); 104 } 105 106 }else{ 107 System.out.println("Unrecognized charset."); 108 } 109 } 110} 111
2、 使用谷歌依赖库来进行判断 https://code.google.com/archive/p/juniversalchardet/