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 ...
随机推荐
- uniq详解
一.简介 报告或删除文件中重复的行. uniq 命令读取由 InFile 参数指定的标准输入或文件.该命令首先比较相邻的行,然后除去第二行和该行的后续副本.重复的行一定相邻.(在发出 uniq 命令之 ...
- Linux CPU 亲和性
在Linux中,我们知道可以通过nice.renice命令改变进程的执行优先级,优先级高的进程优先执行,从而一定程度上保证重要任务的运行. 除了nice.renice外,可以通过CPU affinit ...
- xcode5下面安装Command Line Tools
运行命令 sudo xcode-select --install 就会显示一行文字,并且弹出一个对话框,确认安装后他就会自己下载来安装了. 至此,Command Line Tools就能够重新复活了
- linux下修改防火墙端口对外开放方法
---linix CentOS7的防火墙换成了firewall了,这里做一些记录,下面是一些命令:添加例外端口:# firewall-cmd --add-port=8080/tcp删除例外端口:# f ...
- 十分钟学会写shell脚本
大家好!我是handsomecui,下面我为大家讲解一下shell脚本的写法,讲的不好的地方,欢迎大家留言拍砖. 1.在linux下会写shell脚本是非常重要的,下面我参照例子给大家展示几个脚本,顺 ...
- Rotate It !!(思维)
Rotate It !! Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- The Frog's Games(二分)
The Frog's Games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) ...
- UVA 673 (13.08.17)
Parentheses Balance You are given a string consisting of parentheses () and []. Astring of this ty ...
- fatal error: malformed or corrupted AST file: 'Unable to load module "/Users/apple/Library/Developer
在同一时候安装使用Xcode5, Xcode6之后, 常常遇到这个问题. fatal error: malformed or corrupted AST file: 'Unable to load m ...
- Git 上传本地命令
1.首先建立一个文件夹用以测试 2.在test中写入一个main.c的文件 其内容如下: 3.然后就建立一个git仓库了 4.然后就是把内容加进去了,上传上去 5.然后我们看下git log的信息 6 ...