0

重複の可能性:
エラーを発生させずに Java で ArrayLIst をシリアル化する方法は?

2 つの異なるクラスに 2 つの配列リストがあります。1 つはトランザクション オブジェクトを格納するtemporarilyためのもので、もう 1 つはpermanent. オブジェクトをarraylist からaddAllにコピーするメソッドを使用しています。次に、オブジェクトをarraylist から に保存します。プログラムを再起動すると、arraylists内のオブジェクトが. しかし、私は例外を得ています。私のコードの何が問題なのですか?temporarypermanentpermanentfilepermanentfile

java.io.FileInputStream をインポートします。java.io.FileOutputStream をインポートします。java.io.ObjectInputStream をインポートします。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 の java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156) の java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326) の 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) at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:945)

4

3 に答える 3

5

シリアル化するクラス(この場合は)は、 SerializableインターフェースStockを実装する必要があります。

于 2013-01-06T04:56:47.253 に答える
3

クラスは Serializable を実装するStock必要があり、そのすべてのフィールド (完全な深さまで) は Serializable を実装するか、プリミティブである必要があります。

于 2013-01-06T05:04:19.457 に答える
1

オブジェクトをシリアル化しようとするときは、特定のクラス ( Stock.java) が実装する必要があることを確認java.io.Serializableし、Stock クラスに実装する必要がある他のクラスがある場合java.io.Serializable

于 2013-01-06T05:26:35.447 に答える