怎么用randomAccessFile进行文本文件的倒序输出:我的代码:
import java.io.*;
class resver {
  public static void main(String args[]) {  try {   File f=new File("wo.txt");
 RandomAccessFile a=new RandomAccessFile(f,"rw");
  for(long i=f.length()-1;i>=0;i--)
  { a.seek(i*2);
    System.out.println(""+a.readChar());
   }
    a.close();
    } catch(IOException e) { System.out.print(e);}
 }} 但存在异常,为什么

解决方案 »

  1.   

     a.seek(i*2); 超过了文件尺寸吧
    改成 a.seek(i); 
      

  2.   

    RandomAccessFile
    你此时操作的是文件,是byte[]才对哦!
    他才不管里面存的是啥呢!
      

  3.   

    import   java.io.*; 
    class   resver   { 
        public   static   void   main(String   args[])   { 
         File   f=new   File("wo.txt");     try   {      
      RandomAccessFile   a=new   RandomAccessFile(f,"rw"); 
        for(long   i=f.length()-1;i> =0;i--) 
        {   a.seek(i*2); 
            System.out.println(""+a.readChar()); 
          } 
            a.close(); 
            }   catch(IOException   e)   {   System.out.print(e);} 
      } }
    这样试试~~~
      

  4.   

    import java.io.File;
    import java.io.IOException;
    import java.io.RandomAccessFile;public class Ceshi {
    public static void main(String args[]) { try {
    File f = new File("wo.txt");
    RandomAccessFile a = new RandomAccessFile(f, "rw");
    long i=0;
    String ss=null;
    char[] ch=null;
    while(i<a.length()){
    if(ss==null)
    ss=a.readLine();
    else
    ss=ss.concat("\n"+a.readLine());
    i=a.getFilePointer();
    }
    a.close();
    ch=ss.toCharArray();
    for(i=ch.length-1;i>=0;i--)
    System.out.print(ch[(int)i]);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
      

  5.   

    感谢,但是输出的是乱码,我也想熟练掌握seek()方法,因此我想用seek()
      

  6.   

    当你使用了a.read或者a.read*之类的函数的时候,文件指针a.getFilePointer()自动往后跳了,所以从后往前读的时候seek函数要不断变化,而且文件中还不能出现特殊字符,因为a.getFilePointer()会根据读入的字符类型自动跳过相应的位数
      

  7.   

    你i的初值为  i=f.length()-1 
    然后你又 a.seek(i*2); 
    那这里指针不是跳了两倍总长了?