Android打开Txt中文乱码的解决方案
2016-09-22 20:14
891 查看
原理是找到文档的前三个字节并自动判断文档类型,然后进行转码操作。
原文:
http://blog.csdn.net/handsomedylan/article/details/6138400
亲测可用。
原文:
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文件防止中文乱码终极版(解决方案)
- Linux下打开txt文件中文显示乱码的解决方案
- android读取中文txt乱码 解决方案
- Android 读取TXT 文件中文乱码 解决方案
- 解决gedit打开txt文件乱码问题and解决pdf中文不显示的问题
- ios用UIWebView打开txt乱码的解决方案
- UIWebView 打开txt文档中文不乱码方法
- 解决Ubuntu双击打开 txt 中文乱码(gedit)
- ubuntu 12.04使用gedit打开txt文件中文乱码的处理方法
- android读取中文txt,lrc的乱码(自动判断文档类型并转码)
- 彻底解决android读取中文txt,lrc的乱码(自动判断文档类型并转码)
- 彻底解决android读取中文txt,lrc的乱码(自动判断文档类型并转码)
- linux下打开windows txt文件中文乱码问题
- ubuntu解决 打开windows记事本.txt中文乱码的方法
- 解决Ubuntu双击打开 txt 中文乱码(gedit)
- Android HttpPost数据中文乱码的解决方案
- linux下打开windows txt文件中文乱码问题
- 解决ubuntu打开windows下保存的txt文件中文乱码问题
- android读SDCard中txt文件中文乱码问题
- 彻底解决android读取中文txt的乱码(自动判断文档类型并转码