zoukankan      html  css  js  c++  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; }
  • 相关阅读:
    web访问权限实现方法-探面向对象的编码设计
    解析二进制反码算数求和
    可以把erp当做一个分支-找自己的方向
    电脑开机是怎样自动加载进程
    球管模型和Java
    发明和发现
    漂浮
    js中的一些循环
    ES5中新增的一些方法
    git的一些操作
  • 原文地址:https://www.cnblogs.com/chenhuanBlogs/p/10229413.html
Copyright © 2011-2022 走看看