IO章节的基础代码,不用看完代码,问题只有一个,请看注释~~
import java.io.*;public class UsingRandomAccessFile {
  static String file = "rtest.dat";
  static void display() throws IOException {
    RandomAccessFile rf = new RandomAccessFile(file, "r");
    for(int i = 0; i < 7; i++)
      System.out.println(
        "Value " + i + ": " + rf.readDouble());
    System.out.println(rf.readUTF());
    rf.close();
  }
  public static void main(String[] args)
  throws IOException {
    RandomAccessFile rf = new RandomAccessFile(file, "rw");
    for(int i = 0; i < 7; i++)
      rf.writeDouble(i*1.414);
    rf.writeUTF("The end of the file");
    rf.close();
    display();
    rf = new RandomAccessFile(file, "rw");
    rf.seek(5*8); //这里的 5*8是什么意思(5*8 =40 ,是乘法吗?JDK上的说什么偏移亮搞不懂)
    rf.writeDouble(47.0001);
    rf.close();
    display();
  }
}

解决方案 »

  1.   

    Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.So rf will write 47.000l at the file-pointer.  You can find what happened after the function display() running.
      

  2.   

    seek 
    public void seek(long pos) 
    throws IOException 
    设置到此文件开头测量到的文件指针偏移量,在该位置发生下一个读取或写入操作。偏移量的设置可能会超出文件末尾。偏移量的设置超出文件末尾不会改变文件的长度。只有在偏移量的设置超出文件末尾的情况下对文件进行写入才会更改其长度。每write一个double占用8个byte的长度。seek(5*8)就是把游标(姑且这么认为)定位到第5个8位字节后也就是40的位置。java api doc上对writeDouble好像是有说明的。LZ可以去看一下。