彻底解决android读取中文txt的乱码(自动判断文档类型并转码)
来源:互联网 发布:2017年流行什么网络语? 编辑:程序博客网 时间:2024/06/06 12:29
来源: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; }
0 0
- 彻底解决android读取中文txt的乱码(自动判断文档类型并转码)
- 彻底解决android读取中文txt的乱码(自动判断文档类型并转码
- 彻底解决android读取中文txt,lrc的乱码(自动判断文档类型并转码)
- 彻底解决android读取中文txt,lrc的乱码(自动判断文档类型并转码)
- 彻底解决android读取中文txt,lrc的乱码(自动判断文档类型并转码)
- 彻底解决android读取中文txt,lrc的乱码(自动判断文档类型并转码)
- android读取中文txt,lrc的乱码(自动判断文档类型并转码)
- 解决android读取中文txt的乱码(自动判断文档类型并转码
- 彻底解决Android中文乱码
- 彻底解决Android中文乱码
- Android中文乱码彻底解决
- android读取txt文件中文乱码的问题
- android 读取sd卡的txt并去除乱码
- 彻底解决Android WebView中文乱码的问题
- 解决android读取中文txt乱码问题
- android读取中文txt乱码 解决方案
- Android 读取TXT 文件中文乱码 解决方案
- 自动判断浏览器类型并读取相应CSS的代码
- 基于webkit浏览器,自动获取完整cookie的方法 -- python
- Samsung 6818平台首次编译遇到的问题
- iOS 多线程防止界面跳转卡顿
- 修改Tomcat内存大小
- VIP新人报道
- 彻底解决android读取中文txt的乱码(自动判断文档类型并转码)
- DOM对象转为Jquery对象 Jquery对象转为DOM对象
- 关于Genymotion启动异常、virtualbox 不能为虚拟电脑打开一个新任务/解决Genymotion出现Unable to load VirtualBox engine的问题
- ExecutorService生命周期
- leaflet加载百度地图
- bootstrap中popover.js(弹出框)使用总结+案例
- JQuery之Callback源码分析
- 持续集成 之 Jenkins插件 Multiple SCMs Plugin
- Ionic新建项目(以Android为例)