解决android读取中文txt的乱码(自动判断文档类型并转码
2017-07-13 15:50
459 查看
主要是记录下来,方便使用啊
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 { re 4000 ader = 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,lrc的乱码(自动判断文档类型并转码)
- 彻底解决android读取中文txt的乱码(自动判断文档类型并转码
- 彻底解决android读取中文txt的乱码(自动判断文档类型并转码)
- 彻底解决android读取中文txt,lrc的乱码(自动判断文档类型并转码)
- 彻底解决android读取中文txt,lrc的乱码(自动判断文档类型并转码)
- 彻底解决android读取中文txt,lrc的乱码(自动判断文档类型并转码)
- 彻底解决android读取中文txt的乱码(自动判断文档类型并转码
- android读取中文txt,lrc的乱码(自动判断文档类型并转码)
- android-解决读取txt文件中文乱码问题
- 解决android读取中文txt乱码问题
- android读取txt文件中文乱码的问题
- c# 读取txt文件中文乱码解决方法
- Andoird - 解决读取txt文件中文乱码问题
- Android中文乱码解决文档手册
- android解决读取中文文件乱码的问题
- 【Android】连接网络读取文件内容中文乱码解决办法
- AIR项目读取本地文档以及中文乱码问题的解决
- Silverlight读取包含中文的txt(解决乱码问题)
- Ubuntu 11.04解决txt文档中文乱码方法
- 解决Ubuntu9.10下txt文档中中文文本乱码问题