我正在尝试使用以下方法来读取2个arraylist。从文件中读取两个不同的对象
public static ArrayList<Contestant> readContestantsFromFile() throws IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream("minos.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
ArrayList<Contestant> contestants = (ArrayList<Contestant>) ois.readObject();
ois.close();
return contestants;
}
public static ArrayList<Times> readContestantsFromFile() throws IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream("minos.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
ArrayList<times> times = (ArrayList<Times>) ois.readObject();
ois.close();
return times;
}
这是行不通的。它不能投射到我保存的第二个数组列表类型。那么我怎样才能访问它?我得到确切的错误是这样的:
Exception in thread "main" java.lang.ClassCastException: com.deanchester.minos.model.Contestant cannot be cast to com.deanchester.minos.model.Times
at com.deanchester.minos.tests.testAddTime.main(testAddTime.java:31)
,这是指该生产线是:
ArrayList<times> times = (ArrayList<Times>) ois.readObject();
那么,如何从一个文件中读出2周不同的ArrayList?
为什么不测试它?这通常是找出问题的最佳方法。 –
@Zhehao,我不想测试它,因为想写很多不必要的代码,但现在就做,并且进展顺利。 – Dean