我正在处理一个无法解决的非常痛苦的难题...虽然我已经能够序列化/反序列化单个对象,但我发现自己目前陷入困境。原因是我正在使用对象的 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;
}
}
最佳答案
试试这个:
desrializaedClubs = (ArrayList<FootballClubNL>)in.readObject();
关于java - 反序列化对象的Arraylist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28045225/