我正在解决一个非常痛苦的难题,我无法解决...虽然我能够序列化/反序列化单个对象,但我发现自己目前陷入困境。原因是我正在使用对象的ArrayList,但无法弄清楚...到目前为止,我已经设法将其写入文件FootballClub.ser。以下是我的最佳尝试,但我得到了
线程“主”中的异常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;
}
}
您已序列化一个,List
但正在反序列化到FootballClubNL
。只需更改此行
FootballClubNL club = (FootballClubNL)in.readObject();
到
desrializaedClubs = (ArrayList<FootballClubNL>)in.readObject();
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다