0

可能重复:
如何在 java 中序列化 ArrayLIst 而不会出错?

我在两个不同的类中有两个数组列表。一种是temporarilypermanent. 我使用一种addAll方法将对象从temporaryarraylist 复制到permanent. 然后将对象从permanentarraylist 保存到file. 当我的程序重新启动时,permanentarraylists 中的对象将从file. 但我得到了一个例外。我的代码有什么问题?

导入 java.io.FileInputStream;导入 java.io.FileOutputStream;导入 java.io.ObjectInputStream;导入 java.io.ObjectOutputStream;导入 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) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在 java.lang .reflect.Method.invoke(Method.java:597) 在 java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:945)

4

3 回答 3

5

您要序列化的类(Stock在这种情况下)必须实现Serializable 接口

于 2013-01-06T04:56:47.253 回答
3

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

于 2013-01-06T05:04:19.457 回答
1

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

于 2013-01-06T05:26:35.447 回答