借助JCharDet获取文件字符集
前段时间,在学习lucene的时候,遇到了读取txt文档遇到编码错误的问题。学了几个解决方案,大部分是将文件转十六进制(可以使用UE的Ctrl+H来查看),读取开头的四个标志位来判断。可是总有些文本文件无法识别(我遇到的是部分使用UTF-8编码的文件),后来发现了JCharDet。JCharDet是mozilla(就是firefox那家)的编码识别算法的Java实现,算了,这里是官网,自己看吧。
上代码:
package com.zhyea.util; 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; /**
* 借助JCharDet获取文件字符集
*
* @author robin
*
*/
public class FileCharsetDetector { /**
* 字符集名称
*/
private static String encoding; /**
* 字符集是否已检测到
*/
private static boolean found; private static nsDetector detector; private static nsICharsetDetectionObserver observer; /**
* 适应语言枚举
* @author robin
*
*/
enum Language{
Japanese(1),
Chinese(2),
SimplifiedChinese(3),
TraditionalChinese(4),
Korean(5),
DontKnow(6); private int hint; Language(int hint){
this.hint = hint;
} public int getHint(){
return this.hint;
}
} /**
* 传入一个文件(File)对象,检查文件编码
*
* @param file
* File对象实例
* @return 文件编码,若无,则返回null
* @throws FileNotFoundException
* @throws IOException
*/
public static String checkEncoding(File file) throws FileNotFoundException,
IOException {
return checkEncoding(file, getNsdetector());
} /**
* 获取文件的编码
*
* @param file
* File对象实例
* @param language
* 语言
* @return 文件编码
* @throws FileNotFoundException
* @throws IOException
*/
public static String checkEncoding(File file, Language lang)
throws FileNotFoundException, IOException {
return checkEncoding(file, new nsDetector(lang.getHint()));
} /**
* 获取文件的编码
*
* @param path
* 文件路径
* @return 文件编码,eg:UTF-8,GBK,GB2312形式,若无,则返回null
* @throws FileNotFoundException
* @throws IOException
*/
public static String checkEncoding(String path) throws FileNotFoundException,
IOException {
return checkEncoding(new File(path));
} /**
* 获取文件的编码
*
* @param path
* 文件路径
* @param language
* 语言
* @return
* @throws FileNotFoundException
* @throws IOException
*/
public static String checkEncoding(String path, Language lang)
throws FileNotFoundException, IOException {
return checkEncoding(new File(path), lang);
} /**
* 获取文件的编码
*
* @param file
* @param det
* @return
* @throws FileNotFoundException
* @throws IOException
*/
private static String checkEncoding(File file, nsDetector detector)
throws FileNotFoundException, IOException { detector.Init(getCharsetDetectionObserver()); if (isAscii(file, detector)) {
encoding = "ASCII";
found = true;
} if (!found) {
String prob[] = detector.getProbableCharsets();
if (prob.length > 0) {
encoding = prob[0];
} else {
return null;
}
} return encoding;
} /**
* 检查文件编码类型是否是ASCII型
* @param file
* 要检查编码的文件
* @param detector
* @return
* @throws IOException
*/
private static boolean isAscii(File file, nsDetector detector) throws IOException{
BufferedInputStream input = null;
try{
input = new BufferedInputStream(new FileInputStream(file)); byte[] buffer = new byte[1024];
int hasRead;
boolean done = false;
boolean isAscii = true; while ((hasRead=input.read(buffer)) != -1) {
if (isAscii)
isAscii = detector.isAscii(buffer, hasRead);
if (!isAscii && !done)
done = detector.DoIt(buffer, hasRead, false);
} return isAscii;
}finally{
detector.DataEnd();
if(null!=input)input.close();
}
} /**
* nsDetector单例创建
* @return
*/
private static nsDetector getNsdetector(){
if(null == detector){
detector = new nsDetector();
}
return detector;
} /**
* nsICharsetDetectionObserver 单例创建
* @return
*/
private static nsICharsetDetectionObserver getCharsetDetectionObserver(){
if(null==observer){
observer = new nsICharsetDetectionObserver() {
public void Notify(String charset) {
found = true;
encoding = charset;
}
};
}
return observer;
}
}
这个还存一个问题,就是识别Unicode编码的文件,会返回windows-1252。我使用windows-1252作为编码的时候会报错。
对了,再提供一个这个jar包下载的地址,官网有时会抽风,不能访问。
下载地址:http://download.csdn.net/detail/tianxiexingyun/8286849
就这样。
借助JCharDet获取文件字符集的更多相关文章
- Java如何获取文件编码格式
1:简单判断是UTF-8或不是UTF-8,因为一般除了UTF-8之外就是GBK,所以就设置默认为GBK. 按照给定的字符集存储文件时,在文件的最开头的三个字节中就有可能存储着编码信息,所以,基本的原 ...
- JAVA中获取文件MD5值的四种方法
JAVA中获取文件MD5值的四种方法其实都很类似,因为核心都是通过JAVA自带的MessageDigest类来实现.获取文件MD5值主要分为三个步骤,第一步获取文件的byte信息,第二步通过Messa ...
- java读取resource/通过文件名获取文件类型
java读取resource java读取resource目录下文件的方法: 借助Guava库的Resource类 Resources.getResource("test.txt" ...
- Atitit.检测文本文件的编码 自动获取文件的中文编码
Atitit.检测文本文件的编码 自动获取文件的中文编码 1 不能使用load来检测编码..1 2 使用convert来检测编码1 3 程序检测文本编码2 3.1 根据utf bom头2 3.2 检测 ...
- C++获取文件夹中所有文件
获取文件夹中的文件,用到过很多次,每次用的时候都要去查下,很烦,所以想自己写下,当然,借鉴了很多其他大佬的博客 主要实现的函数,如下: void getFiles( string path, vect ...
- 获取文件的缩略图Thumbnail和通过 AQS - Advanced Query Syntax 搜索本地文件
演示如何获取文件的缩略图 FileSystem/ThumbnailAccess.xaml <Page x:Class="XamlDemo.FileSystem.ThumbnailAcc ...
- FastDFS 通过文件名获取文件信息
/** * 获取文件信息 * * param string group_name 文件所在的组名 * param string file_id 文件id(如: M00/09/BE/rBBZolgj6O ...
- c# .net获取文件夹下的所有文件(多层递归),并获取区间数据(Jsion,xml等数据)写出到处理文件,学习分享~
static void Main(string[] args) { string path = string.Format(@"C:\Users\Administrator\D ...
- java 获取文件列表,并按照文件名称排序
需求:获取全部的日志文件,并按照文件名称倒序排列,把最新的文件放在最前1.获取全部的日志文件:(方法:public List<String> ergodic(File file,List& ...
随机推荐
- table表格超出部分显示省略号
做table表格时,某一列字数比较多,希望超出宽度的部分以省略号显示 设置table的布局 默认automatic 以表格内容显示相应宽度 改成fixed 以表格列宽显示内容 table{ ta ...
- css overflow和float
float:使元素向左或向右移动(不能上下移动),直到它的外边缘碰到包含框或另一个浮动框的边框为止,浮动元素之前的元素将不会受到影响,之后的元素将围绕它. float之后的元素脱离文档流. 默认为no ...
- jQuery的基本过滤器与jQuery实现隔行换色实例
没加过滤器之前: 加过滤器之后: 总的代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8& ...
- be动词
编辑 讨论 be动词,意思和用法很多,一般的意思是:是,此种用法,有多种变化形式,is,am,are,was,were,being,been,to be.另外,be动词还有成为的意思.根据句子中不同的 ...
- MYSQL之MHA集群
环境 manager 192.168.137.141 master1 192.168.137.144 master2 192.168.137.145 slave 192.168.137.141 vip ...
- android设备如何进入深度休眠还能继续使用定时器【求解】
经过试验,andriod设备进入深度休眠的时候,定时器是不能使用.但是阻止设备进入深度休眠,可以获取一把锁,但是拿了锁之后,设备不能进入休眠,系统的功耗会增加.怎么能够在系统进入休眠,定时器还能正常工 ...
- LD_RUN_PATH和LD_LIBRARY_PATH是干什么的?
1. 使用场合 LD_RUN_PATH在链接时使用 LD_LIBRARY_PATH在执行时使用 2. 如何指定环境变量 export LD_LIBRARY_PATH=/opt/jello/lib:$L ...
- bzoj 1251: 序列终结者 平衡树,fhqtreap
链接 https://www.lydsy.com/JudgeOnline/problem.php?id=1251 思路 好简单的模板题 不过还是wrong了好几发 叶子节点要注意下,不能使用 遇到就不 ...
- 【做题】51NOD1753 相似子串——哈希
题意:两个字符串相似定义为: 1.两个字符串长度相等 2.两个字符串对应位置上至多有一个位置所对应的字符不相同 给定一个字符串\(s\),\(T\)次询问两个子串在给定的规则下是否相似.给定的规则指每 ...
- SpringCloud与Consul集成实现负载均衡
一.背景 SpringCloud微服务目前比较流行,其中大都在使用的服务注册与发现是Eureka,最近研究了Consul的集群搭建,现使用Consul实现服务的负载均衡.其主要拓扑结构如下: 二.Co ...