原文:http://blog.csdn.net/handsomedylan/article/details/6138400

public String convertCodeAndGetText(String str_filepath) {// 转码

File file = new File(str_filepath);
                BufferedReader reader;
                String text = "";
                try {
                        // FileReader f_reader = new FileReader(file);
                        // BufferedReader reader = new BufferedReader(f_reader);
                        FileInputStream fis = new FileInputStream(file);
                        BufferedInputStream in = new BufferedInputStream(fis);
                        in.mark(4);
                        byte[] first3bytes = new byte[3];
                        in.read(first3bytes);//找到文档的前三个字节并自动判断文档类型。
                        in.reset();
                        if (first3bytes[0] == (byte) 0xEF && first3bytes[1] == (byte) 0xBB
                                        && first3bytes[2] == (byte) 0xBF) {// utf-8

reader = new BufferedReader(new InputStreamReader(in, "utf-8"));

} else if (first3bytes[0] == (byte) 0xFF
                                        && first3bytes[1] == (byte) 0xFE) {

reader = new BufferedReader(
                                                new InputStreamReader(in, "unicode"));
                        } else if (first3bytes[0] == (byte) 0xFE
                                        && first3bytes[1] == (byte) 0xFF) {

reader = new BufferedReader(new InputStreamReader(in,
                                                "utf-16be"));
                        } else if (first3bytes[0] == (byte) 0xFF
                                        && first3bytes[1] == (byte) 0xFF) {

reader = new BufferedReader(new InputStreamReader(in,
                                                "utf-16le"));
                        } else {

reader = new BufferedReader(new InputStreamReader(in, "GBK"));
                        }
                        String str = reader.readLine();

while (str != null) {
                                text = text + str + "/n";
                                str = reader.readLine();

}
                        reader.close();

} catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
                return text;
        }
代码不难,觉得有用的可以顶一下。

彻底解决android读取中文txt的乱码(自动判断文档类型并转码的更多相关文章

  1. 解决zabbix使用中文是出现乱码的问题

       解决zabbix使用中文是出现乱码的问题 ①:上传windows的simhei.ttf字体到zabbix服务器的/usr/share/zabbix/fonts/目录下   ②:编辑/usr/sh ...

  2. (转)完美解决 Android WebView 文本框获取焦点后自动放大有关问题

    完美解决 Android WebView 文本框获取焦点后自动放大问题 前几天在写一个项目时,要求在项目中嵌入一个WebView 本来很快就完成了,测试也没有问题.但发给新加坡时,他们测试都会出现文本 ...

  3. XML解析之sax解析案例(一)读取contact.xml文件,完整输出文档内容

    一.新建Demo2类: import java.io.File; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXPar ...

  4. C#中StreamReader读取中文文本出现乱码的解决方法

    在编写文本文件读写程序的过程中,有如下代码 StreamReader sr = new StreamReader(FileName); 结果发现打开中文文本文件出现乱码. 究其原因,原来自从Windo ...

  5. Torch-RNN运行过程中的坑 [2](Lua的string sub函数,读取中文失败,乱码?)

    0.踩坑背景 仍然是torch-rnn/LanguageModel.lua文件中的一些问题,仍然是这个狗血的LM:encode_string函数: function LM:encode_string( ...

  6. 解决Mac上打开txt文件乱码问题

    出处:https://www.jianshu.com/p/f55ddf1e9839 经常会在Mac上打开一个txt文件,发现里面的中文都是乱码,问题是在Windows和手机上看都完全是正常的,这就十分 ...

  7. C#中StreamReader读取中文时出现乱码问题总结

    之前有一篇文章" C#读取及写入配置文件教程"(http://blog.csdn.net/lisenyang/article/details/47291083)当中有一个问题就是在 ...

  8. 有效解决ajax传中文时,乱码的情况,php处理接收到的值

    在抽奖环节时,需把获奖名单通过ajax的post方式传输给php后台进行储存,但是php接收到的值确是乱码.在百度之后并没有找到合适的解决方法. 则使用js的encodeURI函数可以有效解决,但不知 ...

  9. 解决python3读写中文txt时UnicodeDecodeError : 'ascii' codec can't decode byte 0xc4 in position 5595: ordinal not in range(128) on line 0的问题

    今天使用python3读写含有中文的txt时突然报了如下错误,系统是MAC OS,iDE是pycharm: UnicodeDecodeError : 'ascii' codec can't decod ...

随机推荐

  1. Java [leetcode 13] Roman to Integer

    问题描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...

  2. Java [leetcode 22]Generate Parentheses

    题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...

  3. 【转】iOS 宏(define)与常量(const)的正确使用-- 不错

    原文网址:http://www.jianshu.com/p/f83335e036b5 在iOS开发中,经常用到宏定义,或用const修饰一些数据类型,经常有开发者不知怎么正确使用,导致项目中乱用宏与c ...

  4. Javascript 对输入框中的内容进行 “全选/反选”

    <</span>script> document.write("<</span>ul>"); for(var i=0;i<&l ...

  5. JMX学习笔记(三)-MXBean

    在MBean中有只要遵循以下两个规则,我们就可以在jconsole中动态的改变MBean中的属性值 1. JMX中要定义接口必须以xxxMBean的规范定义 2. 得有类实现xxxMBean接口 例如 ...

  6. .net 禁止远程查看应用程序错误的详细信息,服务器上出现应用程序错误

    打开页面时出现以下错误   "/"应用程序中的服务器错误. 运行时错误 说明: 服务器上出现应用程序错误.此应用程序的当前自定义错误设置禁止远程查看应用程序错误的详细信息(出于安全 ...

  7. C++基类和派生类之间的转换

    本文讲解内容的前提是派生类继承基类的方式是公有继承,关键字public 以下程序为讲解用例. #include<iostream> using namespace std; class A ...

  8. javaScript动态给下拉列表框添加选项

    方式一: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></titl ...

  9. 浅谈Javascript中默认参数值的设置

    第一种: 1: function test(a,b){ 2: var a = arguments[0] ? arguments[0] : 1;//设置参数a的默认值为1 3: var b = argu ...

  10. Ajax解决缓存的5种方法

    原文:http://www.ido321.com/129.html 1.在ajax发送请求前加上 anyAjaxObj.setRequestHeader(“If-Modified-Since”,”0″ ...