每当我想打印我得到的文件时,我在读取字节时遇到了一些readInt()
问题。当我想显示更新的数字系列时,最后一个 catch 语句中出现了相同的错误。这是我的代码:RandomAccessFile
EOFException
import java.io.*;
import java.util.Scanner;
public class CentNumbers {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
int num;
RandomAccessFile numbers = new RandomAccessFile("Numbers.dat", "rw");
System.out.println("Saved numbers are:");
numbers.setLength(0);
for (int i = 0; i < 100; i++){
num=(int)(Math.random()*100);
numbers.writeInt(num);
System.out.print(numbers.readInt()+" ");
}
try{
do{
System.out.print("\nadd another number? (y/n):");
String x= input.next();
if (!x.equals("y")) throw new Exception("Comeback Soon");
System.out.print("Write number :");
int b=input.nextInt();
numbers.seek(numbers.length());
numbers.writeInt(b);
} while(true);
}//end try
catch ( Exception e){
System.out.println("Saved numbers are:");
for (int i=0; i<numbers.length();i++){
numbers.seek(i*4);
System.out.print(numbers.readInt()+" ");
}//end for
System.out.println();
System.out.println(e.getMessage() );
numbers.close();
}//end catch
}
}