自动识别文件编码
来源:互联网 发布:非主流伤感的网络歌曲 编辑:程序博客网 时间:2024/05/19 03:44
public static Reader readTranslateFile(File bsFile) {
final int BOM_SIZE = 4;
byte[] bom = new byte[BOM_SIZE];
BufferedReader reader = null;
PushbackInputStream pushbackInputStream = null;
try {
// 获取编码
String sourceEncoding = CpdetectorUtil.perceiveCode(bsFile);
// 读取BOM
pushbackInputStream = new PushbackInputStream(new FileInputStream(
bsFile), BOM_SIZE);
int n = pushbackInputStream.read(bom, 0, bom.length);
int unread = n;
// 判断是否有BOM
unread = ishasBOM(bom, n, unread);
// 读取偏移
pushbackInputStream.unread(bom, (n - unread), unread);
reader = new BufferedReader(new InputStreamReader(
pushbackInputStream, sourceEncoding));
return reader;
} catch (MalformedURLException e) {
Logger.getLogger().error(FileUtil.class, e);
} catch (IOException e) {
Logger.getLogger().error(FileUtil.class, e);
}
return null;
}
private static final CodepageDetectorProxy detector = CodepageDetectorProxy
.getInstance();
static {
detector.add(new ParsingDetector(false));
detector.add(JChardetFacade.getInstance());
detector.add(UnicodeDetector.getInstance());
detector.add(ASCIIDetector.getInstance());
}
private CpdetectorUtil() {
}
/**
*
* 感知文件编码方式。
*
* @param 待探测文件
* @return 编码字符串,为空表示没有找到该编码
* @throws IOException
* @throws MalformedURLException
*/
public static String perceiveCode(File file) throws IOException {
if (null == file || !file.exists()) {
return null;
}
Charset charset = null;
charset = detector.detectCodepage(file.toURI().toURL());
if (charset != null) {
return charset.name();
}
return null;
}
cpdetector_1.0.10.jar
chardet-1.0.jar
antlr-2.7.4.jar
- Java 自动识别文件编码
- 自动识别文件编码
- vim自动识别文件编码
- BytesEncodingDetect.java 自动识别文件编码
- 用chardet module自动识别文件编码
- 自动识别编码读取和写入文件
- ASP.NET自动识别GB2312与UTF-8编码的文件
- 自动识别网页编码
- gedit自动识别GBK编码
- 编码自动识别工具 uchardet
- 字符集编码的自动识别jchardet
- IOS TXT文本编码自动识别
- 字符集编码的自动识别jchardet
- C语言自动识别文本编码
- tinyxml自动识别编码的方法
- 浏览器不能自动识别UTF8编码
- python 貌似没法自动识别编码
- 获取指定网页的源码,自动识别编码
- javascript技巧-javascript深入理解js闭包
- android4.1 activity内屏蔽HOME按键功能
- 抽象的操作系统(十) --- 中间件(完)
- C语言字符串转换成日期
- ActionBarSherlock
- 自动识别文件编码
- hive文件存储格式
- 我自己的读书计划----期限一年
- Java新手入门的30个基本概念
- rhel上配置rsync
- GOOGLE PROTOCOL BUFFERS
- linux grep命令
- 每日雕蟲一技[2012-12-28]
- Codeforces Round #111 (Div. 2)——B