Java自动检测文件编码(字符集)


// 使用之前请调用getAllDetectableCharsets()检查是否满足要求,中文仅有{gb18030, big5,utf-*}
import
com.ibm.icu.text.CharsetDetector; import com.ibm.icu.text.CharsetMatch; static HashSet<String> getWhiteList(String fileName) { if (fileName == null) { return null; } HashSet<String> rs = null; InputStreamReader isr = null; BufferedReader br = null; try { FileInputStream fis = new FileInputStream(fileName); BufferedInputStream bis = new BufferedInputStream(fis);// markSupported CharsetMatch charsetMatch = new CharsetDetector().setText(bis).detect(); if (charsetMatch != null) { isr = new InputStreamReader(bis, charsetMatch.getName()); System.out.println("Open '" + fileName + " ' with charset: " + charsetMatch.getName()); } else { isr = new InputStreamReader(bis); System.out.println( "Open '" + fileName + " ' with charset( default, because no charset is detected by IBM.ICU4J): " + isr.getEncoding()); } br = new BufferedReader(isr); String line = null; rs = new HashSet<String>(); while ((line = br.readLine()) != null) { rs.add(line); } } catch (FileNotFoundException e) { System.out.println("WARNING: File '" + fileName + "' is not exist."); } catch (IOException e) { System.out.println("WARNING: IOException occured when read Whitelist."); } finally { try { if (br != null) { br.close(); } } catch (IOException e) { System.out.println("WARNING: IOException occured when close BufferedReader."); } } return rs; }
原文地址:https://www.cnblogs.com/chenhuanBlogs/p/10229413.html