java实现文件编码监测
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实现文件编码监测(转)
chardet是mozilla自动字符集探测算法代码的java移植.这个算法的最初作者是frank Tang,C++源代码在http://lxr.mozilla.org/mozilla/source/ ...
- 使用java进行文件编码转换
在开发过程中,可能会遇到文件编码的转换,尽管说开发工具eclipse能够转换编码,可是有的情况却非常不方便.比方,原来文件本身的编码是GBK,如今要转换成UTF-8,假设直接在eclipse中把文件编 ...
- Java文件编码自动转换工具类(只改变编码,不会改变文件内容)
本篇随笔主要介绍了一个用java语言写的将一个文件编码转换为另一个编码并不改变文件内容的工具类: 通过读取源文件内容,用URLEncoding重新编码解码的方式实现. public class Cha ...
- java文件传输之文件编码和File类的使用
---恢复内容开始--- 我们知道,在用户端和服务端之间存在一个数据传输的问题,例如下载个电影.上传个照片.发一条讯息.在这里我们 就说一下文件的传输. 1.文件编码 相信大家小时候玩过积木(没玩过也 ...
- 文件编码检测.ZC一些资料(包含java的)
1.IMultiLanguage3 或者 IMultiLanguage2 1.1.怎么判断XML 的编码格式(UTF-8或GB2312等)-CSDN论坛.html(https://bbs.csdn.n ...
- 用java修改文件的编码
1.将本地的文件转换成另外一种编码输出,主要逻辑代码如下: /** * 将本地文件以哪种编码输出 * @param inputfile 输入文件的路径 * @param outfile 输出文件的路径 ...
- Java以UTF-8编码读写文件
java中文件操作体现了设计模式中的装饰者模式 . 以utf-8编码写入文件: FileOutputStream fos = new FileOutputStream("test.txt&q ...
- java读取文件并获得文件编码,转换为指定编码的工具类代码
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- 【转载】Java文件编码自动转换工具类
本篇随笔主要介绍了一个用java语言写的将一个文件编码转换为另一个编码并不改变文件内容的工具类: 通过读取源文件内容,用URLEncoding重新编码解码的方式实现. 1 public class C ...
随机推荐
- Intent.ACTION_TIME_TICK 广播
Intent.ACTION_TIME_TICK 广播需要动态注册,不能在清单文件配置. TimeReceiver mBroadcastReceiver = new TimeReceiver(); In ...
- 发几个速度快可以用的google IP,谷歌IP(转)
google搜索引擎打不开时的解决办法,谷歌(google)的IP是多少? google IP镜像. 这里搜集了几个经过测试可用的IP,用来在不能域名访问google的时候进行访问,实时更新! 前面几 ...
- 理解 Linux 配置文件分类和使用
理解 Linux 配置文件分类和使用 本文说明了 Linux 系统的配置文件,在多用户.多任务环境中,配置文件控制用户权限.系统应用程序.守护进程.服务和其它管理任务.这些任务包括管理用户帐号.分配磁 ...
- Search in Rotated Sorted Array (I, II) 解答
Question Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 ...
- Micro Python - Python for microcontrollers
Micro Python - Python for microcontrollers MicroPython
- nodejs学习笔记之安装、入门
由于项目需要,最近开始学习nodejs.在学习过程中,记录一些必要的操作和应该注意的点. 首先是如何安装nodejs环境?(我用的是windows 7环境,所以主要是windows 7的例 ...
- nginx、fastCGI、php-fpm关系梳理(转载参考)
nginx.fastCGI.php-fpm关系梳理 还可以参考:http://www.cnblogs.com/skynet/p/4173450.html 前言: Linux下搭建nginx+php ...
- poj 2229 Sumsets(dp 或 数学)
Description Farmer John commanded his cows to search . Here are the possible sets of numbers that su ...
- hdu 3853 LOOPS(概率 dp 期望)
Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl). Homura wants to help ...
- Tomcat 配置篇
Tomcat 配置一.Tomcat 基本介绍 1.关键目录 a) bin 该目录包含了启动.停止和启动其他的脚本,如startup.sh.shutdown.sh等; b) conf 配置文件和一些文档 ...