TomatoPotato
TomatoPotato

Reputation: 1

How-to serialize / deserialize multiple sets with a differing type into a single file

I have done a bit of research on this subject, however didn't manage to find any solution on my own.

The problem is such: I got three seperate Set objects with differing types.

Example:

private static Set<Vessel> vessels = new HashSet<Vessel>();
private static Set<Seperator> vessels1 = new HashSet<Seperator>();
private static Set<Pipe> vessels2 = new HashSet<Pipe>();

The Vessel, Seperator and Pipe implement Serializable interface.

Simplified example:

public class Vessel implements Serializable{
    private static final long serialVersionUID = 1L;
    int id;
    public Vessel(int id){
        this.id = id;
    }
}

public class Seperator implements Serializable{
    private static final long serialVersionUID = 1L;
    int id;
    public Seperator(int id){
        this.id = id;
    }
}

public class Pipe implements Serializable{
    private static final long serialVersionUID = 1L;

    int id;
    public Pipe(int id){
        this.id = id;
    }
}

I do know how to serialize these sets into seperate files. However, I struggle to find a way to serialize all three independent sets into a single file which ultimately is the objective of my query.

Ideally I am looking for an example on how-to serialize and de-serialize multiple sets with differing types into a single file.

Upvotes: 0

Views: 73

Answers (3)

Amit Parashar
Amit Parashar

Reputation: 1457

The approach suggested by @Vasu should work for you. However this will bring additional cost of deserializing the complete linked list (of sets) when you just want to deserialize the one set. So, a complete solution to your question will be possible in persistent stores like DB, but not on file system. I am pasting the below comment from a previos SO post on a related topic.

Why can't a file that contains multiple appended ObjectOutputStreams be deserialized by one ObjectInputStream?

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.

Upvotes: 1

K139
K139

Reputation: 3669

If you put individual Set objects into file, then you need to remember the order while reading them back from file. So it would be better to put those 3 sets into a class and write that class to avoid that problem. like

class SetValues implements Serializable {

    private static Set<Vessel> vessels = new HashSet<Vessel>();
    private static Set<Seperator> vessels = new HashSet<Seperator>();
    private static Set<Pipe> vessels = new HashSet<Pipe>();

    //getters and setters
}

Upvotes: 0

Vasu
Vasu

Reputation: 2426

Define a LinkedList and add your objects with add. LinkedList implements Serializable, so you can write it to a file. Just make sure to add and read back your sets in the same order.

LinkedList l = new LinkedList();
l.add(vessel);
l.add(vessel1);
l.add(vessel2);

Upvotes: 0

Related Questions