0

I can serialize ArrayList for the first run. It stores the customers arraylist at the first time when the data.ser file is created. The other times it doesn't append new ArrayList of customers.

    public void serializeBank(ArrayList<Customer> newCustomers) {
        try (ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("data.ser", true))) {
            os.writeObject(newCustomers);
        } catch (IOException i) {
            i.getMessage();
        }
    }

Or, do i have to add new customers to the array list of existing customer and pass that updated arraylist to the serialization method? While rewriting a new file everytime a new customer is added works, I want to know if there is a way to append new customers to the existing file.

robinhoodjr
  • 415
  • 8
  • 20

3 Answers3

0

You have to add new customers to the array list of existing customer and pass that updated arraylist to the serialization method.

Rajnish suryavanshi
  • 3,168
  • 2
  • 17
  • 23
0

You should add your new customers to the array list of existing customer and pass it to your method

Thomas Vu
  • 105
  • 1
  • 7
0

At first

You can't append any data to the file using different ObjectOutputStreams. You can read these StackOverflow questions Appending to an ObjectOutputStream & java.io.StreamCorruptedException: invalid type code: AC to understand why you can't do that. But in short it is because each new ObjectOutputStream will append it's header to the file which will corrupt it. So you should use single ObjectOutputStream instance to write all your objects to the file.

Secondly

You can't serialize several Customer objects, and deserialize them as one ArrayList<Customer> object. Because the serialized data of different objects differs a lot. Each serialized data contains metadata about the serialized object type, the field names, field types e.t.c. Somehow ObjectOutputStream marks the start and the end of each serialized object and the ObjectInputStream uses that markers to know how to deserialize the objects. This means that if you'll write 2 objects to ObjectOutputStream your ObjectInputStream will read that 2 objects distinctly.

So, if you want to serialize several Customer objects and then deserialize them as one ArrayList<Customer> then you should implement your own serialization/deserialization logic.

Alternatively you can add new Customer to the ArrayList and then erase all the content of the file and seralize ArrayList to that file, but this will cause performance issues.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Rub
  • 160
  • 2
  • 12
  • Thanks for the detailed explanation. How to deal with the performance issue then? Any suggestions? – robinhoodjr Apr 20 '19 at 07:05
  • I think the best is to write your custom serialization logic.But implementation depends on many factors.For instance "Will you write other data to that file?","Is that list is field of other Serializable object and you actually trying to serialize that parent object?".But, as an example of solution I can suggest to [1] extend `ArrayList` [2] redefine `readObject`, `writeObject` methods [3] track newly added `Customer` objects and serialize only them.But you should be very careful while extending `ArrayList`.Also you should think of deleted `Customer`s -what will happen to them? – Rub Apr 20 '19 at 18:40