从单个文件反序列化多个对象 [英] Deserializing multiple objects from a single file

查看:118
本文介绍了从单个文件反序列化多个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多(同一类)的对象序列化到一个文件中。
但是在反序列化时,只反序列化了第一个序列化对象。

I have a number of objects (of the same class) serialized into a file. But while deserializing it, only the first serialized object is deserialized.

序列化代码:

public void save() {
File f = new File("vehicule.txt");
try {
    if(!f.exists()) f.createNewFile();
} catch(IOException e) {
}
try {
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f,true));
oos.writeObject(this);
} catch(IOException e) {
}

}

我认为问题在于:

Vehicule v;
while( (v = (Vehicule)ois.readObject()) != null )

有没有更好的方法来检查文件的结尾?

Is there a better way to check for the end of the file?

推荐答案

如果你打算使用多个附加的 ObjectOutputStreams ,然后我相信可能有所帮助(同时确保每次运行测试时都删除文件!):

If you are going to use multiple appended ObjectOutputStreams, then I believe this might help (along with making sure you delete the file each time you run your test!):


为什么包含多个附加的ObjectOutputStream的文件不能被一个ObjectInputStream反序列化?

使用序列化的默认实现,必须有
一对一映射 ObjectOutputStream 构造和
ObjectInputStream 构造。 ObjectOutputStream 构造函数
写入一个流头, ObjectInputStream 读取此流
头。解决方法是子类 ObjectOutputStream 并覆盖
writeStreamHeader()。如果第一次写入$ b,则重写 writeStreamHeader()应该
调用超级 writeStreamHeader 方法$ b文件,它应该调用 ObjectOutputStream.reset()如果它是
附加到预先存在的 ObjectOutputStream 在文件中。

Using the default implementation of serialization, there must be a one-to-one mapping between ObjectOutputStream construction and ObjectInputStream construction. ObjectOutputStream constructor writes a stream header and ObjectInputStream reads this stream header. A workaround is to subclass ObjectOutputStream and override writeStreamHeader(). The overriding writeStreamHeader() should call the super writeStreamHeader method if it is the first write to the file and it should call ObjectOutputStream.reset() if it is appending to a pre-existing ObjectOutputStream within the file.

否则我建议您将对象添加到列表然后用一个 ObjectOutputStream 序列化它。

Otherwise I would suggest you add the objects to a List and then serialize it with a single ObjectOutputStream.

例如:

    Vehicule v1 = new Vehicule();
    Vehicule v2 = new Vehicule();
    List<Vehicule> vehicules = Arrays.asList(v1, v2);

    // serialize the list of Vehicules
    File f = new File("vehicule.txt");
    try {
        ObjectOutputStream oos = new ObjectOutputStream(
                new FileOutputStream(f));
        oos.writeObject(vehicules);
        oos.close();
    } catch (Exception e) {
        e.printStackTrace(); // handle this appropriately
    }

    // deserialize the list of Vehicules
    try {
        ObjectInputStream ois = new ObjectInputStream(
                new FileInputStream(f));
        List<Vehicule> deserializedVehicles = (List<Vehicule>) ois.readObject();
        ois.close();
        System.out.println("list size = " + deserializedVehicles.size());
    } catch (Exception e) {
        e.printStackTrace(); // handle this appropriately
    }

对我来说,输出:

list size = 2

这篇关于从单个文件反序列化多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆