程序一:
public class Test08 {
public static void main(String[] args) throws Exception {
RandomAccessFile raf=new RandomAccessFile("raf.dat", "rw");
int max=Integer.MAX_VALUE;
long lon=Long.MAX_VALUE;
raf.writeInt(max);
raf.seek(0);
System.out.println(raf.readInt());
System.out.println(raf.getFilePointer());
//插入代码raf.seek(4);
raf.writeLong(lon);
System.out.println(raf.readLong());
}
}
执行结果:
2147483647
4
Exception in thread "main" java.io.EOFException
at java.io.RandomAccessFile.readInt(Unknown Source)
at java.io.RandomAccessFile.readLong(Unknown Source)
at day06.Test08.main(Test08.java:23)
程序二:
public class Test08 {
public static void main(String[] args) throws Exception {
RandomAccessFile raf=new RandomAccessFile("raf.dat", "rw");
int max=Integer.MAX_VALUE;
long lon=Long.MAX_VALUE;
raf.writeInt(max);
raf.writeLong(lon);
raf.seek(0);
System.out.println(raf.readInt());
System.out.println(raf.getFilePointer());
System.out.println(raf.readLong());
}
}
结果:2147483647
4
9223372036854775807
两者只是 raf.writeLong(lon); 位置不同,为什么一个包异常,一个正常运行,程序一插入代码任有问题,这是为何啊?
四无小青年
相关分类