簡體   English   中英

用不同的對象寫arraylist到文件

[英]Write arraylist with different objects to file

我正在嘗試將一個包含不同對象的ArrayList寫入文件。 這是我的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。 我希望你們看到為什么拋出這個異常。

已經謝謝了!

由於您的ArrayList包含Coach實例,您只需要使Coach類實現java.io.Serializable (可能還有其他類,如GoalkeeperFielder )。

Serializable Javadoc說:

遍歷圖形時,可能會遇到不支持Serializable接口的對象。 在這種情況下,將拋出NotSerializableException,並將標識非可序列化對象的類。

但是,Serialization在Java中的工作方式相當復雜。 有關完整信息,請參閱http://docs.oracle.com/javase/6/docs/platform/serialization/spec/serialTOC.html

ArrayList確實可序列化,但ArrayList的序列化功能還需要能夠序列化列表中的所有對象。 這意味着列表中的所有對象也必須實現Serializable 在這種情況下,至少Coach不可序列化(並且可能不是列表中的其他對象)因此拋出異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM