java自动探测文件的字符编码
Mozilla有一个C++版的自动字符集探测算法代码,然后sourceforge上有人将其改成java版的~~
主页:http://jchardet.sourceforge.net/
jchardet is a java port of the source from mozilla's automatic charset detection algorithm.
The original author is Frank Tang. What is available here is the java port of that code.
The original source in C++ can be found from http://lxr.mozilla.org/mozilla/source/intl/chardet/
More information can be found at http://www.mozilla.org/projects/intl/chardet.html
下面是见证奇迹的时刻:
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; public class FileCharsetDetector {
private boolean found = false;
private String encoding = null; public static void main(String[] argv) throws Exception {
File file1 = new File("C:\\test1.txt"); System.out.println("文件编码:" + new FileCharsetDetector().guessFileEncoding(file1));
} /**
* 传入一个文件(File)对象,检查文件编码
*
* @param file
* File对象实例
* @return 文件编码,若无,则返回null
* @throws FileNotFoundException
* @throws IOException
*/
public String guessFileEncoding(File file) throws FileNotFoundException, IOException {
return guessFileEncoding(file, new nsDetector());
} /**
* <pre>
* 获取文件的编码
* @param file
* File对象实例
* @param languageHint
* 语言提示区域代码 @see #nsPSMDetector ,取值如下:
* 1 : Japanese
* 2 : Chinese
* 3 : Simplified Chinese
* 4 : Traditional Chinese
* 5 : Korean
* 6 : Dont know(default)
* </pre>
*
* @return 文件编码,eg:UTF-8,GBK,GB2312形式(不确定的时候,返回可能的字符编码序列);若无,则返回null
* @throws FileNotFoundException
* @throws IOException
*/
public String guessFileEncoding(File file, int languageHint) throws FileNotFoundException, IOException {
return guessFileEncoding(file, new nsDetector(languageHint));
} /**
* 获取文件的编码
*
* @param file
* @param det
* @return
* @throws FileNotFoundException
* @throws IOException
*/
private String guessFileEncoding(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) {
encoding = charset;
found = true;
}
}); BufferedInputStream imp = new BufferedInputStream(new FileInputStream(file));
byte[] buf = new byte[1024];
int len;
boolean done = false;
boolean isAscii = false; while ((len = imp.read(buf, 0, buf.length)) != -1) {
// Check if the stream is only ascii.
isAscii = det.isAscii(buf, len);
if (isAscii) {
break;
}
// DoIt if non-ascii and not done yet.
done = det.DoIt(buf, len, false);
if (done) {
break;
}
}
imp.close();
det.DataEnd(); if (isAscii) {
encoding = "ASCII";
found = true;
} if (!found) {
String[] prob = det.getProbableCharsets();
//这里将可能的字符集组合起来返回
for (int i = 0; i < prob.length; i++) {
if (i == 0) {
encoding = prob[i];
} else {
encoding += "," + prob[i];
}
} if (prob.length > 0) {
// 在没有发现情况下,也可以只取第一个可能的编码,这里返回的是一个可能的序列
return encoding;
} else {
return null;
}
}
return encoding;
}
}
上面是判断文件编码的demo,本人测试了一下,得到的结果还是比较靠谱的~
上面提到的主页上还有一个HtmlCharsetDetector的demo,感兴趣的话可以去看一下。
java自动探测文件的字符编码的更多相关文章
- python学习笔记(2)--列表、元组、字符串、字典、集合、文件、字符编码
本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1.列表和元组的操作 列表是我们以后最长用的数据类型之一,通过列表可以最方便的对数据实现最方便的存储.修改等操作 定 ...
- Day2 - Python基础2 列表、字符串、字典、集合、文件、字符编码
本节内容 列表.元组操作 数字操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 ...
- python基础之 列表、元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码
本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 ...
- linux下改变文件的字符编码
首先确定文件的原始字符编码: $ file -bi test.txt 然后用 iconv 转换字符编码 $ iconv -f from-encoding -t to-encoding file > ...
- Gnu Linux下文件的字符编码及转换工具
/********************************************************************* * Author : Samson * Date ...
- eclipse设置新建jsp文件默认字符编码为utf-8
在使用Eclipse开发中,编码默认是ISO-8859-1,不支持中文.这样我们每次新建文件都要手动修改编码,非常麻烦.其实我们可以设置文件默认编码,今后再新建文件时就不用修改编码了. 1.打开Ecl ...
- fedora23深度配置gnome系统环境, 如设置ibus的面板字体大小 以及gedit 自动探测文件字符编码fileencodings
除了系统桌面gnome, 以及gnome应用程序自带的preferences, 还有很多设置, 没有在preferences, 而是被深度地隐藏在系统中, 这时, 需要安装 dconf-tools: ...
- python 读写文件和设置文件的字符编码
一. python打开文件代码如下: f = open("d:\test.txt", "w") 说明:第一个参数是文件名称,包括路径:第二个参数是打开的模式mo ...
- Java Web---登录验证和字符编码过滤器
什么是过滤器? 在Java Web中,过滤器即Filter.Servlet API中提供了一个Filter接口(javax.servlet.Filter).开发web应用时,假设编写的Java类实现了 ...
随机推荐
- JSON Web Token in ASP.NET Web API 2 using Owin
In the previous post Decouple OWIN Authorization Server from Resource Server we saw how we can separ ...
- leetcode 152. 乘积最大子序列 java
题目: 给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数). 示例 1: 输入: [2,3,-2,4] 输出: 6 解释: 子数组 [2,3] 有最大乘积 6. ...
- 通过DHCP动态管理IP地址
DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)是一个局域网的网络协议,使用UDP协议工作, 主要有两个用途:给内部网络或网络服务供应商自动分配IP ...
- maven build的常用生命周期
常用的maven build goals: validate - validate the project is correct and all necessary information is av ...
- django~项目的文件位置的重要性
前几天我犯了个很低级的错误 就是把文件的地址放错地方了~~ 我把templates文件放进mysite文件里面了 和templatetags文件同级了 所以一直报错 说找不到模板的文件 实际上te ...
- Centos6.7配置Nginx+Tomcat简单整合
系统环境:Centos 6.7 软件环境:JDK-1.8.0_65.Nginx-1.10.3.Tomcat-8.5.8 文档环境:/opt/app/ 存放软件目录,至于mkdir创建文件就不用再说了 ...
- 剑指offer九之变态跳台阶
一.题目 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 二.思路 1.关于本题,前提是n个台阶会有一次n阶的跳法.分析如下: f(1) ...
- IntelliJ Idea注释模板--类注释、方法注释
刚从Eclipse切换到IntelliJ Idea,之前使用eclipse时用到了注释模板,包括类注释和方法注释,现在分别讲一下在Intellij Idea中如何进行配置,作为备忘 一. 类注释模板配 ...
- SwitchHosts—hosts管理工具
SwitchHosts是一个管理.快速切换Hosts小工具,开源软件,一键切换Hosts配置,非常实用,高效.开发Web过程成,部署有多套环境,网址域名都相同,部署在不同的服务器上,有开发环境.测试环 ...
- javascript实现代码高亮-wangHighLighter.js
1. 引言 (先贴出wangHighLighter.js的github地址:https://github.com/wangfupeng1988/wangHighLighter注意,程序和使用说明的更新 ...