1

我正在尝试将包含不同对象的 ArrayList 写入文件。这是我的数组列表:

ArrayList<Person> personList = new ArrayList<Person>();
    private Person theCaptain;


    void init(){

    //Adding persons to the list

    personList.add(new Coach("Tesan de Boer", "Vieruslaan 3b", "0689337554"));
    personList.add(new Goalkeeper("Peter Post", "Eeuwige student 66", "2222", 1));
    personList.add(new Goalkeeper("piet puk", "Weg van ongenade 88", "2222", 21));
    personList.add(new Goalkeeper("Siem van Aanhoolt", "Straatweg 45", " 0612213446", 31));
    personList.add(new Fielder("Koen Weegink", "Straatweg 45", "2222", 2));
    personList.add(new Fielder("Jan-Willem Rufus op den Haar", "Straatweg 45", " 0614226698", 3));
    personList.add(new Fielder("Tom Kraniker", "Straatweg 45", "069873663", 4));
    personList.add(new Fielder("Leon het Kanon", "Straatweg 45", "2222", 6));
    personList.add(new Fielder("Robin Hogezant", "Straatweg 45", "2222", 7));
    personList.add(new Fielder("Loesoe de Kat", "Straatweg 45", "222", 8));
    personList.add(new Fielder("Morris de Spee", "Straatweg 45", "2222", 9));
    personList.add(new Fielder("Rein Zoekers", "Straatweg 45", "2222", 10));
    personList.add(new Fielder("Darion Pok", "Straatweg 45", "2222", 11));
    personList.add(new Fielder("Achmed de Bom", "Straatweg 45", "2222", 12)); 

    }

在这里,我试图将该 ArrayList 写入一个名为 test 的文件:

public class Main {

public static void main(String[] args)throws Exception {

    Team team = new Team();
    team.init();
    ArrayList<Person> playersData = team.getPersonList();

    try {

       // create a new file with an ObjectOutputStream
       FileOutputStream out = new FileOutputStream("test.txt");
       ObjectOutputStream oout = new ObjectOutputStream(out);

       // write something in the file
       oout.writeObject(playersData);

       // close the stream
       oout.close();

       // create an ObjectInputStream for the file we created before
       ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));



    } catch (Exception ex) {
       ex.printStackTrace();
    }

}

}

当我运行程序时,我得到这个错误:

java.io.NotSerializableException: Coach
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at java.util.ArrayList.writeObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at Main.main(Main.java:24)

我以为ArrayList实现了Serializable,所以不知道为什么会抛出NotSerializableException。我希望你们明白为什么会抛出这个异常。

已经谢谢了!

4

2 回答 2

2

由于您ArrayList包含Coach实例,因此您只需使Coach类实现java.io.Serializable(可能还有您正在使用的其他类,如Goalkeeperor Fielder)。

序列化的 Javadoc说:

在遍历图时,可能会遇到不支持 Serializable 接口的对象。在这种情况下,NotSerializableException 将被抛出,并将识别不可序列化对象的类。

尽管如此,序列化在 Java 中的工作方式还是相当复杂的。有关完整信息,请参阅http://docs.oracle.com/javase/6/docs/platform/serialization/spec/serialTOC.html

于 2013-06-22T15:25:20.290 回答
1

ArrayList确实是可序列化的,但是 的序列化功能ArrayList也需要能够序列化列表中的所有对象。这意味着列表中的所有对象也必须实现Serializable。在这种情况下,至少Coach是不可序列化的(可能列表中的其他对象也不是可序列化的),因此会引发异常。

于 2013-06-22T15:24:55.757 回答