如何存储和;检索文件中对象的arraylist?

时间:2013-01-06 04:52:24

标签: java file-io arraylist

  

可能重复:
  How to Serialize an ArrayLIst in java without getting errors?

我在两个不同的班级中有两个arraylists。一种是存储交易对象temporarily&其他permanent。我使用addAll方法将对象从temporary arraylist复制到permanent。然后将对象从permanent arraylist保存到file。当我的程序重新启动时,permanent arraylists中的对象将从file恢复。但我得到一个例外。我的代码怎么了?

  

import java.io.FileInputStream; import java.io.FileOutputStream;   import java.io.ObjectInputStream; import java.io.ObjectOutputStream;   import java.util.ArrayList;

     

/ ** * * @author Haleemdeen * / public class FileImportExport {

private ArrayList<Stock> permntTransactions=new ArrayList<Stock>();


void cancatToPerrmntTransactions(ArrayList<Stock> arraylist1){
    permntTransactions.addAll(arraylist1);
}

ArrayList<Stock> displayPermntTransactions(){
    return permntTransactions;
}

    void exportToFile(){

    try{  // Catch errors in I/O if necessary.
    // Open a file to write to, named SavedObj.sav.
    FileOutputStream saveFile=new FileOutputStream("SaveObj.sav");

    // Create an ObjectOutputStream to put objects into save file.
    ObjectOutputStream save = new ObjectOutputStream(saveFile);

    // Now we do the save.
    save.writeObject(permntTransactions);

    // Close the file.
    save.close(); // This also closes saveFile.
    }
    catch(Exception exc){
    exc.printStackTrace(); // If there was an error, print the info.
    }
}

        void importFromFile(){

    try{
    // Open file to read from, named SavedObj.sav.
    FileInputStream saveFile = new FileInputStream("SaveObj.sav");

    // Create an ObjectInputStream to get objects from save file.
    ObjectInputStream save = new ObjectInputStream(saveFile);

    // Now we do the restore.
    // readObject() returns a generic Object, we cast those back
    // into their original class type.

    permntTransactions = (ArrayList<Stock>) save.readObject();

    // Close the file.
    save.close(); // This also closes saveFile.
    }
    catch(Exception exc){
    exc.printStackTrace(); // If there was an error, print the info.
    }

    // Print the values, to see that they've been recovered.
    System.out.println("\t\t" + permntTransactions);
    System.out.println();
} }

主要方法:

FileImportExport file1=new FileImportExport();
file1.cancatToPerrmntTransactions(transactions);
file1.exportToFile();

这是我得到的例外:

  

java.io.NotSerializableException:m_media_cdstore.Stock           at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)           at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)           at java.util.ArrayList.writeObject(ArrayList.java:570)           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)           在java.lang.reflect.Method.invoke(Method.java:597)           在java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:945)

3 个答案:

答案 0 :(得分:5)

要序列化的类(在本例中为Stock)必须实现Serializable interface

答案 1 :(得分:3)

Stock类必须实现Serializable,并且其所有字段(完全深度)必须实现Serializable或者是原始的。

答案 2 :(得分:1)

尝试序列化对象时,请确保特定类(Stock。java)必须实现java.io.Serializable,如果Stock类具有必须实现的任何其他类java.io.Serializable