java实现文件编码监测
2014-10-06 15:51
302 查看
java实现文件编码监测
最近在做一个文档的翻译项目,可文档的编码不知道,听头疼的。尝试了很多方法最后发现JCharDet这个工具可以轻松解决这个问题。于是作此笔记希望日后提醒自己以及帮助又需要的人。package com.uujava.mbfy.test; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import org.mozilla.intl.chardet.nsDetector; import org.mozilla.intl.chardet.nsICharsetDetectionObserver; /********************************************** * Maven * <!-- 用于文件编码检查 --> * <dependency> * <groupId>net.sourceforge.jchardet</groupId> * <artifactId>jchardet</artifactId> * <version>1.0</version> * </dependency> * *********************************************/ /** * 借助JCharDet获取文件字符集 JCharDet * 是mozilla自动字符集探测算法代码的java移植,其官方主页为: * http://jchardet.sourceforge.net/ */ public class FileCharsetDetector { private boolean found = false; /** * 如果完全匹配某个字符集检测算法, 则该属性保存该字符集的名称. * 否则(如二进制文件)其值就为默认值 null, 这时应当查询属性 */ private String encoding = null; public static void main(String[] argv) throws Exception { System.out .println("文件编码:" + new FileCharsetDetector() .guestFileEncoding("/home/k/Documents/test/azmind_7_xh/azmind_7_xh/路由管理.txt")); } /** * 传入一个文件(File)对象,检查文件编码 * * @param file * File对象实例 * @return 文件编码,若无,则返回null * @throws FileNotFoundException * @throws IOException */ public String guestFileEncoding(File file) throws FileNotFoundException, IOException { return geestFileEncoding(file, new nsDetector()); } /** * 获取文件的编码 * * @param file * File对象实例 * @param languageHint * 语言提示区域代码 eg:1 : Japanese; 2 : Chinese; 3 : Simplified Chinese; * 4 : Traditional Chinese; 5 : Korean; 6 : Dont know (default) * @return 文件编码,eg:UTF-8,GBK,GB2312形式,若无,则返回null * @throws FileNotFoundException * @throws IOException */ public String guestFileEncoding(File file, int languageHint) throws FileNotFoundException, IOException { return geestFileEncoding(file, new nsDetector(languageHint)); } /** * 获取文件的编码 * * @param path * 文件路径 * @return 文件编码,eg:UTF-8,GBK,GB2312形式,若无,则返回null * @throws FileNotFoundException * @throws IOException */ public String guestFileEncoding(String path) throws FileNotFoundException, IOException { return guestFileEncoding(new File(path)); } /** * 获取文件的编码 * * @param path * 文件路径 * @param languageHint * 语言提示区域代码 eg:1 : Japanese; 2 : Chinese; 3 : Simplified Chinese; * 4 : Traditional Chinese; 5 : Korean; 6 : Dont know (default) * @return * @throws FileNotFoundException * @throws IOException */ public String guestFileEncoding(String path, int languageHint) throws FileNotFoundException, IOException { return guestFileEncoding(new File(path), languageHint); } /** * 获取文件的编码 * * @param file * @param det * @return * @throws FileNotFoundException * @throws IOException */ private String geestFileEncoding(File file, nsDetector det) throws FileNotFoundException, IOException { // Set an observer... // The Notify() will be called when a matching charset is found. det.Init(new nsICharsetDetectionObserver() { public void Notify(String charset) { found = true; encoding = charset; } }); BufferedInputStream imp = new BufferedInputStream(new FileInputStream(file)); byte[] buf = new byte[1024]; int len; boolean done = false; boolean isAscii = true; while ((len = imp.read(buf, 0, buf.length)) != -1) { // Check if the stream is only ascii. if (isAscii) isAscii = det.isAscii(buf, len); // DoIt if non-ascii and not done yet. if (!isAscii && !done) done = det.DoIt(buf, len, false); } det.DataEnd(); if (isAscii) { encoding = "ASCII"; found = true; } if (!found) { String prob[] = det.getProbableCharsets(); if (prob.length > 0) { // 在没有发现情况下,则取第一个可能的编码 encoding = prob[0]; } else { return null; } } return encoding; } }
相关文章推荐
- java实现文件编码监测(转)
- java实现文件编码格式的转化
- Java 过滤器读取配置文件实现过滤编码
- INI文件格式以及Java编码实现读取
- 识别常见编码格式文件并转换成UTF-8编码的java实现
- Java实现将GBK编码格式的文件夹中所有文件都转化为UTF-8格式的文件,编码格式转化
- 识别常见编码格式文件并转换成UTF-8编码 的java实现 源码
- java网络编程--解析通过web.xml文件控制利用过滤器实现编码格式步骤
- 利用Java实现对文件的简易Base64编码
- 利用DirectShow实现对视频文件H264编码与解码基类
- 用Java简单实现文件分割与合并
- java中如何实现文件打包上传以及自动解压
- Java实现压缩一个目录下所有图片文件
- 动态规划解决矩阵链乘问题的java编码实现
- newxy技术零java代码实现文件下载,下载记数 作者:胡立新
- 用java实现对文件的各种操作
- java从数据库导数据生成txt文件的中文编码问题
- java实现对文件的各种操作
- Visual Basic Base64编码源码:可实现对字符串(中文)和二进制文件编码
- 一个将数据文件转换成excel文件打印的java实现方法的代码片断(Struts+poi)