反序列化对象的 Arraylist

Deserialize Arraylist of objects

我正在处理一个我无法解决的极其痛苦的难题...虽然我已经能够 serialize/deserialize 单个对象,但我发现自己此刻被困住了。原因是我正在处理对象的 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;
    }

}

您的文件包含 FootballClubNL 的列表,而这正是从 in.readObject() 返回的内容,因此您必须将其转换为 ArrayList<FootballClubNL>

您序列化了 List,但反序列化为 FootballClubNL。只需更改此行

FootballClubNL club = (FootballClubNL)in.readObject();

desrializaedClubs = (ArrayList<FootballClubNL>)in.readObject();

试试这个:

desrializaedClubs = (ArrayList<FootballClubNL>)in.readObject();

将其转换为 ArrayList

    List<FootballClubNL> clubs = (ArrayList<FootballClubNL> )in.readObject();

您正在尝试反序列化数组 int FootballClubNL.. 尝试以下

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

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);

        **ArrayList<FootballClubNL> club = (ArrayList<FootballClubNL>)in.readObject();**

        return club;
    }

}

也可以获取对象。这只是虚拟代码...您可以这样做:

Object readObject = in.readObject();;
if(readObject instanceof FootballClubNL){
   FootballClubNL club = (FootballClubNL) readObject;
}

在这里你可以检查读取的对象是否是一个足球俱乐部的实例,然后它会投射它!