现在的位置: 首页 > 综合 > 正文

使用RandomAccessFile流将一个文本文件倒置读出

2013年08月25日 ⁄ 综合 ⁄ 共 1005字 ⁄ 字号 评论关闭

       如题,这个应该并不是很难,难的是如果中文和外文混合的话不知道怎么读出,我的代码只能实现英文读出,如果中文和外文混杂的话就会读出乱码。如果某年某月某日某时你看到了这篇文章,并且可以处理现存的多种文字的话请在下面留言,多谢……

Code:
  1. import java.io.*;   
  2. import java.util.*;   
  3.   
  4. public class TestRandomAccessFile {   
  5.     public static final void main(String[] args) {   
  6.         try {   
  7.             File f = new File("exp.txt");   
  8.             if(!f.exists()) {   
  9.                 f.createNewFile();   
  10.             }   
  11.             RandomAccessFile raf = new RandomAccessFile(f,"rw");   
  12.             String str = new String();   
  13.             str = new Scanner(System.in).next();   
  14.             for(int i=0; i<str.length(); i++) {   
  15.                 raf.write(str.charAt(i));   
  16.             }   
  17.             raf.writeUTF(str);   
  18.             long len = str.length();   
  19.             while(0 != len--) {   
  20.                 raf.seek(len);   
  21.                 char ch = (char)raf.read();   
  22.                 System.out.println(ch);   
  23.             }   
  24.             raf.close();   
  25.         } catch(IOException e) {   
  26.             e.printStackTrace();   
  27.         }   
  28.     }   
  29. }

抱歉!评论已关闭.