我在已保存的file.dat中有一个对象数组,我想将它们恢复为与我的java程序相同的对象数组,以便将它们用于我的程序 这是我的代码,但根本不起作用
public static void main (String args[]) throws FileNotFoundException, IOException {
Seat[] arr = new Seat [100];
try{
File f = new File("reservedSeats.dat");
FileInputStream fs = new FileInputStream(f);
DataInputStream ds = new DataInputStream(fs);
int temp;
for(int i=0;i<arr.length;i++){
temp=ds.readInt();
arr[i] = new Seat (String.valueOf(temp)/*to convert from int to String*/);
}
}
catch ( FileNotFoundException e ){
}
for (Seat arr1 : arr) {
System.out.println(arr1);
}
我的Seat.java构造函数如下:
public Seat(String seatNo)
{
this.seatNo = seatNo;
}
答案 0 :(得分:0)
尝试使用ObjectInputStream:
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("reservedSeats.dat"));
Seat[] lst = (Seat[]) ois.readObject();