Android--推断文本文件编码
方法1:利用windows文本文件编码特点。
windows下。Unicode、Unicode big endian和UTF-8编码的txt文件的开头会多出几个字节,各自是FF、FE(Unicode),FE、FF(Unicode big endian),EF、BB、BF(UTF-8)。
public static String getCharset(File file) {
String charset = "GBK";
byte[] first3Bytes = new byte[3];
try {
boolean checked = false;
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file));
bis.mark(0);
int read = bis.read(first3Bytes, 0, 3);
if (read == -1)
return charset;
if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) {
charset = "UTF-16LE";
checked = true;
} else if (first3Bytes[0] == (byte) 0xFE && first3Bytes[1]
== (byte) 0xFF) {
charset = "UTF-16BE";
checked = true;
} else if (first3Bytes[0] == (byte) 0xEF && first3Bytes[1]
== (byte) 0xBB
&& first3Bytes[2] == (byte) 0xBF) {
charset = "UTF-8";
checked = true;
}
bis.reset();
if (!checked) {
int loc = 0;
while ((read = bis.read()) != -1) {
loc++;
if (read >= 0xF0)
break;
//单独出现BF下面的。也算是GBK
if (0x80 <= read && read <= 0xBF)
break;
if (0xC0 <= read && read <= 0xDF) {
read = bis.read();
if (0x80 <= read && read <= 0xBF)// 双字节 (0xC0 - 0xDF)
// (0x80 -
// 0xBF),也可能在GB编码内
continue;
else
break;
// 也有可能出错,可是几率较小
} else if (0xE0 <= read && read <= 0xEF) {
read = bis.read();
if (0x80 <= read && read <= 0xBF) {
read = bis.read();
if (0x80 <= read && read <= 0xBF) {
charset = "UTF-8";
break;
} else
break;
} else
break;
}
}
System.out.println(loc + " " + Integer.toHexString(read));
}
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
return charset;
}
缺点:不能这样去探測linux下的文件。
方法2:开源project JCharDet
http://www.iteye.com/topic/266501
package org.mozilla.intl.chardet; import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; /**
* 借助JCharDet获取文件字符集
* @author icer
* PS:
* JCharDet 是mozilla自己主动字符集探測算法代码的java移植,其官方主页为:
* http://jchardet.sourceforge.net/
* @date 2008/11/13
*/
public class FileCharsetDetector { private boolean found = false; /**
* 假设全然匹配某个字符集检測算法, 则该属性保存该字符集的名称. 否则(如二进制文件)其值就为默认值 null, 这时应当查询属性
*/
private String encoding = null; public static void main(String[] argv) throws Exception {
if (argv.length != 1 && argv.length != 2) { System.out
.println("Usage: FileCharsetDetector <path> [<languageHint>]"); System.out.println("");
System.out.println("Where <path> is d:/demo.txt");
System.out.println("For optional <languageHint>. Use following...");
System.out.println(" 1 => Japanese");
System.out.println(" 2 => Chinese");
System.out.println(" 3 => Simplified Chinese");
System.out.println(" 4 => Traditional Chinese");
System.out.println(" 5 => Korean");
System.out.println(" 6 => Dont know (default)"); return;
} else {
String encoding = null;
if (argv.length == 2) {
encoding = new FileCharsetDetector().guestFileEncoding(argv[0],
Integer.valueOf(argv[1]));
} else {
encoding = new FileCharsetDetector().guestFileEncoding(argv[0]);
}
System.out.println("文件编码:" + encoding);
}
} /**
* 传入一个文件(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;
}
}
jar包下载地址:http://download.csdn.net/detail/u012587637/8047697
方法3:开源projectjuniversalcharde
http://code.google.com/p/juniversalchardet/
public static String getFileIncode(File file) { if (!file.exists()) {
System.err.println("getFileIncode: file not exists!");
return null;
} byte[] buf = new byte[4096];
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
// (1)
UniversalDetector detector = new UniversalDetector(null); // (2)
int nread;
while ((nread = fis.read(buf)) > 0 && !detector.isDone()) {
detector.handleData(buf, 0, nread);
}
// (3)
detector.dataEnd(); // (4)
String encoding = detector.getDetectedCharset();
if (encoding != null) {
System.out.println("Detected encoding = " + encoding);
} else {
System.out.println("No encoding detected.");
} // (5)
detector.reset();
fis.close();
return encoding;
} catch (Exception e) {
e.printStackTrace();
} return null;
}
引入包的方法:
将包放入libs目录。
选中包,右键 --> build path--> add to build path。
jar包下载:http://download.csdn.net/detail/u012587637/8041181
说明:第三个方法要比第二个速度快些,也比較新,所以推荐使用第三个。
Android--推断文本文件编码的更多相关文章
- android Java BASE64编码和解码二:图片的编码和解码
1.准备工作 (1)在项目中集成 Base64 代码,集成方法见第一篇博文:android Java BASE64编码和解码一:基础 (2)添加 ImgHelper 工具类 package com.a ...
- android TextView Unicde编码转换 android中一些特殊字符Unicode码值
android TextView Unicde编码转换 android中一些特殊字符Unicode码值 android中一些特殊字符(如:←↑→↓等箭头符号,约等于号≍)的Unicode码值 Text ...
- Windows文本文件编码
目录 1 ANSI编码 2 2 UTF16BE编码 2 3 UTF16LE编码 2 4 UTF-8编码 2 5 BOM 3 6 乱码 3 7 总结 5 如下图 ...
- android Java BASE64编码和解码一:基础
今天在做Android项目的时候遇到一个问题,需求是向服务器上传一张图片,要求把图片转化成图片流放在 json字符串里传输. 类似这样的: {"name":"jike&q ...
- android 推断Apk是否签名和 签名是否一致
推断Apk是否签名 用命令:jarsigner -verify -verbose -certs <apk文件> 假设有Android Debug字樣就是debug 假设已经签名: [证书的 ...
- 自动判断文本文件编码来读取文本文件内容(.net版本和java版本)
.net版本 using System; using System.IO; using System.Text; namespace G2.Common { /// <summary> / ...
- android推断手机是否root
关于推断手机是否已经root的方法.假设app有一些特殊功能须要root权限,则须要推断是否root. 比方一些市场下载完app后自己主动安装. /** * @author Kevin Kowalew ...
- java自动识别用户上传的文本文件编码
原文:http://www.open-open.com/code/view/1420514359234 经常碰到用户上传的部分数据文本文件乱码问题,又不能限制用户的上传的文件编码格式(这样对客户的要求 ...
- [Android]推断网络连接是否可用
/** * 推断移动网络是否开启 * * @param context * @return */ public static boolean isNetEnabled(Context context) ...
随机推荐
- php 获取ip地址方法
function getIP() { $ip=""; if(!empty($_SERVER['HTTP_CLIENT_IP'])){ $myip = $_SERVER['HTTP_ ...
- 【Hadoop】01_从官网下载Hadoop
在[Linux]Ctentos下载我已经描述了如何去下载Centos 进入到Hadoop官网 http://hadoop.apache.org/ 点击"releases",跳转后, ...
- Python-Mac OS X EI Capitan下安装Scrapy
sudo pip install scrapy --ignore-installed six #sudo pip install scrapy --upgrade --ignore-installed ...
- 如何使用Xcode进行高保真原型设计?
转载自:http://www.guimobile.net/xcode-high-fidelity-prototype-design.html Xcode不仅是开发者用来开发iOS Apps的开发工具, ...
- 在做和sap系统集成的一点心得
最这一个月和sap做集成,把主数据中的数据写入到sap系统中,做集成就离不开联调,平时开发中,一个复杂点的系统还容易出问题,涉及到两个系统,由于两个或多个系统来自不同的厂家,各个厂家的开发对业务的理解 ...
- canvas学习笔记(中篇) -- canvas入门教程-- 颜色/透明度/渐变色/线宽/线条样式/虚线/文本/阴影/图片/像素处理
[中篇] -- 建议学习时间4小时 课程共(上中下)三篇 此笔记是我初次接触canvas的时候的学习笔记,这次特意整理为博客供大家入门学习,几乎涵盖了canvas所有的基础知识,并且有众多练习案例, ...
- CentOS7关闭默认防火墙启用iptables防火墙
CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙步骤. 1.关闭firewall: systemctl stop firewalld.service #停止f ...
- 使用Parallel实现简单的并行操作
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...
- jmeter ---集合点使用方法:Synchronizing Timer
LR中集合点可以设置多个虚拟用户等待到一个点,同时触发一个事务,以达到模拟真实环境下多个用户同时操作,实现性能测试的最终目的. jmeter中使用Synchronizing Timer实现Lr中集合点 ...
- 【学习笔记】C#中的单元测试
周一老师讲完单元测试以后,感觉挺好玩,通过查资料和相关书籍学到了几种C#在VS2010的测试方法,跟大家分享下,图文并茂啊,有木有~~ 1.从被测试的代码中生成单元测试 1.1创建C#控制台程序,命名 ...