1. 程式人生 > >Java自動檢測檔案編碼(字符集)

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; }