【发布时间】:2015-03-18 16:10:55
【问题描述】:
我正在处理一个我无法解决的非常痛苦的难题......虽然我已经能够序列化/反序列化单个对象,但我发现自己现在陷入困境。原因是我正在使用对象的 ArrayList,但无法弄清楚...到目前为止,我已经设法将其写入文件 FootballClub.ser。下面这个是我最好的尝试,但我得到了
线程“main”中的异常 java.lang.ClassCastException:java.util.ArrayList 无法转换为 FootballClubNL
public class FootballClubFileWriter {
private static final String filename_1 = "FootballClub.ser";
private static final String filename_2 = "FootballClub.txt"; //Use for question 5
public static void serializeToDisk(ArrayList<FootballClubNL> clubs) throws IOException {
FileOutputStream fos = new FileOutputStream(filename_1);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(clubs);
fos.close();
oos.close();
}
public static ArrayList<FootballClubNL> deserializeFromDisk()throws IOException, ClassNotFoundException {
ArrayList<FootballClubNL> desrializaedClubs = new ArrayList<FootballClubNL>();
FileInputStream fileIn = new FileInputStream("FootballClub.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
FootballClubNL club = (FootballClubNL)in.readObject();
return desrializaedClubs;
}
}
【问题讨论】:
标签: java object serialization arraylist