下面是编程之家 jb51.cc 通过网络收集整理的代码片段。
编程之家小编现在分享给大家,也给大家做个参考。
package com.dxx.buscredit.common.util; import info.monitorenter.cpdetector.io.ASCIIDetector; import info.monitorenter.cpdetector.io.CodepageDetectorProxy; import info.monitorenter.cpdetector.io.JChardetFacade; import info.monitorenter.cpdetector.io.ParsingDetector; import info.monitorenter.cpdetector.io.UnicodeDetector; import java.io.File; import java.nio.charset.Charset; public class FileCharsetDetector { /** * 利用第三方开源包cpdetector获取文件编码格式. * @param filePath * @return */ public static String getFileEncode(File file) { /** * <pre> * 1、cpDetector内置了一些常用的探测实现类,这些探测实现类的实例可以通过add方法加进来,* 如:ParsingDetector、 JChardetFacade、ASCIIDetector、UnicodeDetector. * 2、detector按照“谁最先返回非空的探测结果,就以该结果为准”的原则. * 3、cpDetector是基于统计学原理的,不保证完全正确. * </pre> */ CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance(); detector.add(new ParsingDetector(false)); detector.add(UnicodeDetector.getInstance()); detector.add(JChardetFacade.getInstance());//内部引用了 chardet.jar的类 detector.add(ASCIIDetector.getInstance()); Charset charset = null; try { charset = detector.detectCodepage(file.toURI().toURL()); } catch (Exception e) { e.printStackTrace(); } //默认为GBK String charsetName = "GBK"; if (charset != null) { if (charset.name().equals("US-ASCII")) { charsetName = "ISO_8859_1"; } else{ charsetName = charset.name(); } } return charsetName; } }来自:http://blog.csdn.net/ysola4/article/details/42442357
以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。