【问题标题】:Deserialize Arraylist of objects反序列化对象的Arraylist
【发布时间】:2015-03-18 16:10:55
【问题描述】:

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

}

【问题讨论】:

    标签: java object serialization arraylist


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        【解决方案3】:

        试试这个:

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

        【讨论】:

          【解决方案4】:

          将其转换为 ArrayList

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

          【讨论】:

          • 这甚至无法编译。
          【解决方案5】:

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

          【讨论】:

            【解决方案6】:

            也可以获取对象。这只是虚构的代码......你可以这样做:

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

            在这里您可以检查读取的对象是否是足球俱乐部的实例,然后它会强制转换它!

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2020-08-02
              • 2015-05-29
              • 2015-06-28
              • 2014-02-08
              • 1970-01-01
              • 2013-01-18
              • 2019-07-10
              • 1970-01-01
              相关资源
              最近更新 更多