A
A
Alexey24112020-08-25 09:54:28
Java
Alexey2411, 2020-08-25 09:54:28

Java serialization. How to write multiple objects to a file?

Is it possible to serialize two instances of the same class into one output stream (file)? And if so, how?

public class Main {

    public static void main(String[] args) {

        try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("person.dat", false))) {
            Person p = new Person("Sam", 33, 178, true);
            Person p2 = new Person("John", 40, 165, false);
            oos.writeObject(p);
            oos.writeObject(p2);
        } catch(Exception ex) {
            System.out.println(ex.getMessage());
        }

        try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.dat"))) {
            Person p = (Person) ois.readObject();
            System.out.println(p.getName());
            System.out.println(p.getAge());
            System.out.println(p.getHeight());
            System.out.println(p.getMarried());
        } catch (Exception e) {
            e.fillInStackTrace();
        }
    }
}

class Person implements Serializable {

    private static final long serialVersionUID = 1L;

    private String name;
    private int age;
    private double height;
    private boolean married;

    Person(String name, int age, double height, boolean married) {
        this.name = name;
        this.age = age;
        this.height = height;
        this.married = married;
    }

    String getName() { return name; }
    int getAge() { return age; }
    double getHeight() { return height; }
    boolean getMarried() { return married; }
}


Output:
Sam
33
178.0
true

It is not clear why this instance was counted ( I guess because it was written first ). If you call .readObject() again, the same instance is read.

UPD: Issue resolved. You just need to sequentially first write, and then read the objects.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2020-08-25
@Aleksey2411

public class Main {

    public static void main(String[] args) {

        try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("person.dat", false))) {
            Person p = new Person("Sam", 33, 178, true);
            Person p2 = new Person("John", 40, 165, false);
            oos.writeObject(p);
            oos.writeObject(p2);
        } catch(Exception ex) {
            System.out.println(ex.getMessage());
        }

        try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.dat"))) {
            Person p = (Person) ois.readObject();
            System.out.println(p.getName());
            System.out.println(p.getAge());
            System.out.println(p.getHeight());
            System.out.println(p.getMarried());

            Person p2 = (Person) ois.readObject();
            System.out.println(p2.getName());
            System.out.println(p2.getAge());
            System.out.println(p2.getHeight());
            System.out.println(p2.getMarried());
        } catch (Exception e) {
            e.fillInStackTrace();
        }
    }
}

Output:
Sam
33
178.0
true
John
40
165.0
false
Maybe I didn't understand the question?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question