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; }
  • 相关阅读:
    使用Docker在本地搭建Hadoop分布式集群
    微博推荐 第三个map 源码
    对象
    http无状态(stateless)
    理解http的无连接
    http响应报文之首部行
    http响应报文之状态行
    http响应报文
    http请求报文之首部行
    http请求之请求数据
  • 原文地址:https://www.cnblogs.com/chenhuanBlogs/p/10229413.html
Copyright © 2011-2022 走看看