0

我有这些类:“MyClass1”、“MyClass2”、“MyClass3”和“MyMainClass”,

public class MyMainClass implements Serializable {
     private String att1, att2, att3;
     private int att4, att5, att6;
     private LinkedList <MyClass1> myClass1List = new LinkedList<MyClass1>();
     private LinkedList <MyClass2> myClass2List = new LinkedList<MyClass2>();
     private LinkedList <MyClass3> myClass3List = new LinkedList<MyClass3>();
}

我的程序创建“MyMainClass”的寄存器(对象)并将其存放在 LinkedList 中。我想在重新打开程序后将对象的 LinkedList 保存在文件中以获取它们。有什么办法呢?我尝试过使用 ObjectOutputStream,但不起作用。谢谢。

编辑:我添加对象的代码(我刚刚阅读了一个示例并尝试了):

public static void addObject (MyMainClass p) {
    try {
        outputStream = new ObjectOutputStream(new FileOutputStream("myfile.dat"));
        outputStream.writeObject(p);
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
        System.exit(1);
    } catch (IOException ex) {
        ex.printStackTrace();
        System.exit(1);
    } finally {
        try {
            if (outputStream != null) {
                outputStream.flush();
                outputStream.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
            System.exit(1);
        }
    }
}

注意:“MyClass1”、“MyClass2”、“MyClass3”是可序列化的。

4

3 回答 3

1

I used following for my highschool project long time ago. Due to my poor English skills I do not really understand what class you wish to save and load (LinkedList or myMainClass), but I used this solution to successfully store and load any of my custom objects. I hope you find it handy.

Usage:

myMainClass object;
//
// ... your code fillin up the content of object
//
MyIO io = new MyIO();
io.save("", "myfile.dat", object); // "" as first argument will make java use current working directory

// to load the object:

myMainObject object = (myMainObject) io.load("", "myfile.dat"); 

Source:

import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;

public class MyIO {

    // String path - path to the directory where the file is supposed to be saved.
    // String filename - the name of the file
    // Object data - object that you wish to save in the file. In your case "myMainClass"

    public void save(String path, String filename, Object data) {
        try {
            FileOutputStream fos = new FileOutputStream(path + filename, false);
            GZIPOutputStream gzos = new GZIPOutputStream(fos);
            ObjectOutputStream out = new ObjectOutputStream(gzos);
            out.writeObject(data);
            out.flush();
            out.close();
        } catch (IOException e) {
            System.out.println(e);
        }
    }

    // String path - path to the directory where the file is stored
    // String filename - the name of the file
    // The function returns java object which can be cast to myMainClass.

    public Object load(String path, String filename) {
        try {
            FileInputStream fis = new FileInputStream(path + filename);
            GZIPInputStream gzis = new GZIPInputStream(fis);
            ObjectInputStream in = new ObjectInputStream(gzis);
            Object data = in.readObject();
            in.close();
            return data;
        } catch (Exception e) {
            System.out.println(e);
        }
        return null;
    }
}
于 2013-04-08T16:38:32.673 回答
1

我会制作“myClass1”、“myClass2”和“myClass3” Serializable,然后将myClass1ListmyClass2ListmyClass3List(以及您要保存的任何其他数据)包装在另一个可序列化的类中,这样您就可以使用序列化/反序列化来保存和恢复所有程序状态一次。

除非myMainClass是那个包装器,在这种情况下,您需要声明它们都实现了可序列化。

于 2013-04-08T16:49:37.220 回答
1

myMainClass没有标记Serializable。另外,myClass1,myClass2和也可以myClass3序列化吗?如果不是,他们应该是。

另一方面,请遵循 Java 命名约定;类名应以大写字母开头。

更新

您确定它没有写入文件,还是代码抛出了您看不到的异常?

在您的所有catch块中,您都有System.exit(1),它绝对不会为您提供有关正在发生的任何异常的信息;你基本上是在吞食它们。您至少应该打印出堆栈跟踪 ( ex.printStackTrace()),这样您就可以看到出了什么问题。

于 2013-04-08T16:52:48.147 回答