反序列化对象的Arraylist

时间:2015-01-20 12:18:12

标签: java object serialization arraylist

我正在处理一个我无法解决的完全痛苦的谜题......虽然我已经能够序列化/反序列化单个对象,但我发现自己当时陷入困境。原因是我正在使用对象的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;
    }

}

6 个答案:

答案 0 :(得分:2)

试试这个:

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

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

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

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

答案 3 :(得分:1)

您正在尝试反序列化一个数组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;
    }

}

答案 4 :(得分:0)

也可以获得Object。它只是虚构的代码......你可以这样做:

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

在这里你可以查看被引导的对象是否是一个足球俱乐部的实例,而不是它会投射它!

答案 5 :(得分:-2)

将其转换为ArrayList

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