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) ...
随机推荐
- 网络编程入坑基础-BIO总结
IO总结 前提 参考资料: <Java I/O> -- 这本书没有翻译版,需要自己啃一下. <Java I/O>这本书主要介绍了IO和NIO的相关API使用,但是NIO部分并不 ...
- android 从Activity 获取 rootView 根节点
private static View getRootView(Activity context) { return ((ViewGroup)context.findViewById(android. ...
- JavaScript 中的string 方法
创建string的方法 var str ="abc"; var str = new String("abc"); var str = String(" ...
- Thread.sleep(0)的作用
我们可能经常会用到 Thread.Sleep 函数来使线程挂起一段时间.那么你有没有正确的理解这个函数的用法呢?思考下面这两个问题: 假设现在是 2008-4-7 12:00:00.000,如果我调用 ...
- 基于redis的简易分布式爬虫框架
代码地址如下:http://www.demodashi.com/demo/13338.html 开发环境 Python 3.6 Requests Redis 3.2.100 Pycharm(非必需,但 ...
- 中国版Azure支持那些版本号Linux
不在下述列表中的Linux表示尚未经过正式验证,并不意味着不能使用,客户能够通过自行上传镜像文件的方式使用其它Linux版本号,可是不保证是否遇到一些驱动或者兼容问题. 分发 版本号 上次验证时间 驱 ...
- 分析USB平台设备模型框架(1)
start_kernel rest_init(); kernel_thread(kernel_init, NULL, CLONE_FS | CLONE_SIGHAND); do_basic_setup ...
- 自定义基于XML的验证器
a.编写一个类,继承FieldValidatorSupport类. b.在public void validate(Object object)编写你的验证逻辑 不符合要求的就向fieldErrors ...
- 【干货】电路设计师指导手册(已更新完毕)(转载EDN)
[干货]电路设计师指导手册(已更新完毕) 第一部分:接地与布线第二部分:电源返回路径与I/O信号接地第三部分:板间互连.星形接地及屏蔽第四部分:安全地以及电线/电缆第五部分:射频电缆.双绞线与串扰
- monogdb windows环境下 安装及使用简单示例
1,下载地址:http://www.mongodb.org/downloads,选择windows平台,当前最新的版本是:2.6.4,本机是64位win7系统,我选择的是windows 64位msi, ...