解决RandomAccessFile.readLine()读取中文乱码
2013-10-30 00:00
441 查看
/** * 倒叙读取文集最后30行 * @param filename * @return */ public static String read(String filename) { String runMessage=""; RandomAccessFile rf = null; try { rf = new RandomAccessFile(filename, "r"); long len = rf.length(); long start = rf.getFilePointer(); long nextend = start + len; String line; rf.seek(nextend); int c = -1; int x = 0; while ((nextend > start) & (x < 30)) { c = rf.read(); if (c == '\n' || c == '\r') { line = rf.readLine(); if (line != null) { //System.out.println(line); runMessage+=new String(line)+"<br>"; } else { System.out.println(line);// 输出为null,可以注释掉 } nextend--; x++; } nextend--; rf.seek(nextend); if (nextend == 0) {// 当文件指针退至文件开始处,输出第一行 System.out.println(rf.readLine()); runMessage+=rf.readLine()+"<br>"; } } //System.out.println(runMessage); runMessage=new String(runMessage.getBytes("8859_1"),"gb2312");//解决中文乱码 } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (rf != null) rf.close(); } catch (IOException e) { e.printStackTrace(); } } return runMessage; }
相关文章推荐
- RandomAccessFile readLine 方法读取中文乱码
- RandomAccessFile写入时中文乱码解决方案
- BufferedRandomAccessFile--解决RandomAccessFile读取大文件时效率低的问题
- 通过RandomAccessFile实现MyRandomAccessFile.readLine()方法--消除乱码
- QT读取access是数据库表名中文乱码
- java读取properties配置文件时中文乱码解决办法
- 文章标题 解决IDEA读取txt文本中显示的中文乱码问题
- spring使用@Value标签读取.properties文件的中文乱码问题的解决
- java中利用RandomAccessFile读取超大文件
- 关于解决StreamReader读取中文乱码的解决方法
- 采用ISO8211封装的S57数据,中文读取时乱码及丢字原因分析与解决方法,终极解决方案.
- C#中StreamReader读取中文文本出现乱码的解决方法(转)
- 采用ISO8211封装的S57数据,中文读取时乱码及丢字原因分析与解决方法-更正与补充
- java读取.properties文件及解决中文乱码问题
- php读取xml中文乱码解决方法
- MYSQL读取中文正常插入乱码(?)的解决和在DOS下insert,select中文的方法
- FileReader读取中文txt文件编码丢失问题(乱码)(转)
- 读取zip文件,不解压缩直接解析,支持文件名中文,解决内容乱码(支付宝对账单为例)
- CStdioFile UNICODE编译 读取中文汉字乱码解决办法
- flash+ashx(aspx)读取xml中文乱码解决方法