我正在嘗試使用以下方法來讀取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