java实现文件编码监测(转)

简介: chardet是mozilla自动字符集探测算法代码的java移植。这个算法的最初作者是frank Tang,C++源代码在http://lxr.mozilla.org/mozilla/source/intl/chardet/,可以从http://www.mozilla.org/projects/intl/chardet.html 得到更多关于这个算法的信息。

 chardet是mozilla自动字符集探测算法代码的java移植。这个算法的最初作者是frank Tang,C++源代码在http://lxr.mozilla.org/mozilla/source/intl/chardet/,可以从http://www.mozilla.org/projects/intl/chardet.html 得到更多关于这个算法的信息。

 

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; } }
http://www.cnblogs.com/mxcy/p/4008342.html
相关文章
|
24天前
|
Java
有关Java发送邮件信息(支持附件、html文件模板发送)
有关Java发送邮件信息(支持附件、html文件模板发送)
25 1
|
27天前
|
Java
java中替换文件内容
java中替换文件内容
13 1
|
29天前
|
Java API
Java中文件与输入输出
Java中文件与输入输出
|
29天前
|
Java
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
9 0
|
1月前
|
Java
java程序导出堆文件
java程序导出堆文件
|
1月前
|
SQL Oracle Java
sql文件批处理程序-java桌面应用
sql文件批处理程序-java桌面应用
25 0
|
1月前
|
存储 Java 文件存储
如何用 Java 压缩 ZIP 文件?
【2月更文挑战第21天】
29 1
|
1月前
|
Java
Java实现文件和目录的管理
Java实现文件和目录的管理
24 0
|
25天前
|
Java 数据库连接 API
Java 学习路线:基础知识、数据类型、条件语句、函数、循环、异常处理、数据结构、面向对象编程、包、文件和 API
Java 是一种广泛使用的、面向对象的编程语言,始于1995年,以其跨平台性、安全性和可靠性著称,应用于从移动设备到数据中心的各种场景。基础概念包括变量(如局部、实例和静态变量)、数据类型(原始和非原始)、条件语句(if、else、switch等)、函数、循环、异常处理、数据结构(如数组、链表)和面向对象编程(类、接口、继承等)。深入学习还包括包、内存管理、集合框架、序列化、网络套接字、泛型、流、JVM、垃圾回收和线程。构建工具如Gradle、Maven和Ant简化了开发流程,Web框架如Spring和Spring Boot支持Web应用开发。ORM工具如JPA、Hibernate处理对象与数
90 3
|
29天前
|
Java
使用java将字符串写入到指定的文件中
使用java将字符串写入到指定的文件中
11 0