日期:2014-05-20  浏览次数:20934 次

诚心请教
写了下面一段代码,感觉没问题可就是编译老不通过:
import   java.io.*;
public   class   random_file
{     public   static   void   main(String[]   args)
      {   int   data_arr[]={12,31,56,23,27,1,43,65,4,99};
            try   {   RandomAccessFile   randf=new   RandomAccessFile   ( "temp.txt ");
                                                                           
                for   (int   i=0;i <data_arr.length;i++)
                        randf.writeInt(data_arr[i]);
                for(int   i=data_arr.length-1;i> =0;i--)
                {         randf.seek(i*4);
                          System.out.println(randf.readInt());       }
                randf.close();
          }catch   (IOException   e)
          {       System.out.println( "File   access   error:   "+e);}     }   }


编译提示:
E:\JavaChengXu\random_file.java:5:   找不到符号
符号:   构造函数   RandomAccessFile(java.lang.String)
位置:   类   java.io.RandomAccessFile
            try   {   RandomAccessFile   randf=new   RandomAccessFile   ( "temp.txt ");
                                                                      ^
1   错误

处理已完成。



------解决方案--------------------
try { RandomAccessFile randf=new RandomAccessFile ( "temp.txt ");
用eclipse看到的错误,没有这个构造方法
The constructor RandomAccessFile(String) is undefined

源代码定义了这2个实现:
public RandomAccessFile(String name, String mode)
public RandomAccessFile(String name, String mode)
------解决方案--------------------
RandomAccessFile randf = new RandomAccessFile( "temp.txt ", "rw ");
定义mode为读写应该是可以用的
------解决方案--------------------
。。2楼那个帖我打错字了,应该是这2个构造器
public RandomAccessFile(File file, String mode)
public RandomAccessFile(String name, String mode)