一个RandomAccess中的point问题
import java.io.*;
public class RandomAccess {
public static void main(String[] args) throws IOException {
RandomAccessFile books = new RandomAccessFile("books.dat", "rw");
String [] bookList = {"Starship Troopers", "The Hobbit", "The Man in the High Castle"};
int [] yearList = {1959, 1954, 1974};
long[] booknamepoint=new long[3];
long[] bookyearpoint=new long[3];
for(int i=0; i<3; i++) {
books.writeUTF(bookList[i]);
booknamepoint[i]=books.getFilePointer();
System.out.println("书名位置:"+booknamepoint[i]);
books.writeInt(yearList[i]);
bookyearpoint[i]=books.getFilePointer();
System.out.println("年份位置:"+bookyearpoint[i]);
}
books.seek(0);
for(int i=0;i<bookList.length-1;i++){
if(i==0){
books.seek(0);
}else{books.seek(bookyearpoint[i-1]);}
books.readUTF();
books.readInt();
System.out.println(books.readUTF() + " " + books.readInt());
}
books.close();
}
}
结果是
The Hobbit 1954
The Man in the High Castle 1974
为什么不是
Starship Troopers 1959
The Hobbit 1954
?