【发布时间】:2011-07-05 19:57:01
【问题描述】:
我正在尝试使用以下方法读取 2 个数组列表。
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 个不同的数组列表呢?
【问题讨论】:
-
你为什么不测试一下?这通常是找出答案的最佳方式。
-
@Zhehao,我不想测试它,因为我想写很多不必要的代码,但现在做起来,进展顺利。