解决android读取中文txt的乱码(自动判断文档类型并转码
来源:互联网 发布:网络推广产品代理 编辑:程序博客网 时间:2024/05/17 00:08
主要是记录下来,方便使用啊
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; }
阅读全文
0 0
- 解决android读取中文txt的乱码(自动判断文档类型并转码
- 彻底解决android读取中文txt的乱码(自动判断文档类型并转码
- 彻底解决android读取中文txt,lrc的乱码(自动判断文档类型并转码)
- 彻底解决android读取中文txt,lrc的乱码(自动判断文档类型并转码)
- android读取中文txt,lrc的乱码(自动判断文档类型并转码)
- 彻底解决android读取中文txt,lrc的乱码(自动判断文档类型并转码)
- 彻底解决android读取中文txt,lrc的乱码(自动判断文档类型并转码)
- 彻底解决android读取中文txt的乱码(自动判断文档类型并转码)
- 解决android读取中文txt乱码问题
- android-解决读取txt文件中文乱码问题
- 解决Ubuntu11.10 TXT文档中文乱码的问题
- 解决Ubuntu11.10 TXT文档中文乱码的问题
- 一行一行的读取txt中文文本(解决乱码问题)
- Silverlight读取包含中文的txt(解决乱码问题)
- PHP读取TXT中文乱码的解决方式
- PHP读取TXT中文乱码的解决方式
- 解决前端读取txt文档乱码
- python读取txt解决中文乱码问题
- 剑指offer面试题42_2 左旋转字符串
- (wdlinux)安装Redis扩展
- 几款按钮样式
- C/C++
- js:jquery插件jquery.form.js options简介
- 解决android读取中文txt的乱码(自动判断文档类型并转码
- RocketMQ原理解析-broker 4.HA & master slave
- HDU -- 2121 【无定根的最小树形图】
- 算法设计与分析笔记——合并排序
- ubuntu14.04 Mysql5.6主从,使用percona保证数据一致
- Z-Stack中LED控制
- sublime 快速编码技巧
- 最全的分页查询
- 面试总结