简体   繁体   中英

Deserialize Arraylist of objects

I am dealing with a utterly painful puzzle I cannot solve... While I have been able to serialize/deserialize single objects I find myself stuck at the moment. The reason is that I am working with an ArrayList of objects, and cannot figure it out... So far I have managed to write it to a file, FootballClub.ser. This one below is my best attempt, but I get

Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList cannot be cast to 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();

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

You serialized a List but you are deserializing to FootballClubNL . Just change this line

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

to

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

You are trying to deserialize an array int FootballClubNL.. try the following

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

}

It's also possible to get the Object. It's only fictiv code... You can do something like this:

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

Here you can check out if the readed object is an instance of an footballclub, than it will cast it!

将其转换为ArrayList

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM