There are several problems been faced which are as follows:
- When you write an object in a file using serialization in java for the first time, no problem arises in reading the file afterward, even when you write multiple objects in one go.
- Now, when next time you try to append new objects (of the same type) to that file using serialization, writing the file will be done successfully without any error.
- But, reading the file will create a problem and an exception named as StreamCorruptedException will be thrown.
The root cause behind these problems or we can say the reasons are as follows:
- Whenever we open a file & try to append a serializable object to the end of the file using ObjectOutputStream & FileOutputStream, ObjectOutputStream will write the header to the end of the file to write the object data. Each time when the file gets open and the first object is written, ObjectOutputStream will write the header to the end of the file prior to the writing of object data.
- So, in this way header gets written multiple times whenever the file is opened in append mode to write the object using FileOutputStream & ObjectOutputStream.
In order to fix these issues, several measures are needed to be implemented as follows:
- Create your own Object Output Stream class, say MyObjectOutputStream class, by extending ObjectOutputStream (Inheritance) & Override the method : “protected void writeStreamHeader() throws IOException.” In your new class, this method should do nothing.
- Now when you write Object for the first time, i.e, when file length is 0, use object of Predefined class ObjectOutputStream, to write the object using writeObject().
- This will write the header to the file in the beginning.
Next time whenever you write the object, i.e, when file length is > 0, use the object of Your defined class MyObjectOutputStream, to write the object using writeObject(). As you have overridden the writeStreamHeader() method & it does nothing, the header will not be written again in the file.
Here in order to optimize the program, to get understanding in one go, we will be having 3 different java class files corresponding to their executable java classes
Example 1: CustomerCollection.java
For now, save this code in a file CustomerCollection.java
Example 2: Customer.java
For now, save this code in a file Customer.java
Example 3: Main.java
For now, save this code in a file Main.java
Output: After saving all the 3 files, run the program
Note: Output will be different after the succeeding run trials.
Run 1: When the program is run for the first time the output is as follows:
****Reading File**** Rita & 1 Sita & 2
Run 2: Again when the above program is run, then there is a difference and the output is as follows:
****Reading File**** Rita & 1 Sita & 2 Rita & 1 Sita & 2
The output is so because already in the first time, these 2 objects (having name Rita & Sita) were written in the file named “BankAccountt.txt” and when you run the code a second time, again the same 2 objects get append in the file.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.