Open In App

How to Fix java.io.StreamCorruptedException: invalid type code in Java?

Improve
Improve
Like Article
Like
Save
Share
Report

There are several problems been faced which are as follows:

  1. 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.
  2. 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.
  3. 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:

  1. 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.
  2. 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: 

  1. 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.
  2. 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().
  3. 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.

Implementation:

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

  1. CustomerCollection.java
  2. Customer.java
  3. Main.java

Example 1: CustomerCollection.java

Java




// Java program to illustrate CustomerCollection.java
 
// Importing input output classes
import java.io.*;
// Importing utility classes
import java.util.*;
 
// Class 1
// helper class
class MyObjectOutputStream extends ObjectOutputStream {
 
    // Constructor of this class
    // 1. Default
    MyObjectOutputStream() throws IOException
    {
 
        // Super keyword refers to parent class instance
        super();
    }
 
    // Constructor of this class
    // 1. Parameterized constructor
    MyObjectOutputStream(OutputStream o) throws IOException
    {
        super(o);
    }
 
    // Method of this class
    public void writeStreamHeader() throws IOException
    {
        return;
    }
}
 
// Class 2
// Helper class
public class CustomerCollection {
 
    // Getting file from local machine by creating
    // object of File class
    private static File f = new File("BankAccountt.txt");
 
    // Method 1
    // To read from the file
    public static boolean readFile()
    {
        // Initially setting bool value as false
        boolean status = false;
 
        // Try block to check for exceptions
        try {
 
            // Creating new file using File object above
            f.createNewFile();
        }
 
        // Catch block to handle the exception
        catch (Exception e) {
        }
 
        // If the file is empty
        if (f.length() != 0) {
 
            try {
 
                // If file doesn't exists
                FileInputStream fis = null;
 
                fis = new FileInputStream(
                    "BankAccountt.txt");
                ObjectInputStream ois
                    = new ObjectInputStream(fis);
 
                Customer c = null;
 
                while (fis.available() != 0) {
                    c = (Customer)ois.readObject();
                    long accNo = c.getAccountNumber();
 
                    // Print customer name and account
                    // number
                    System.out.println(c.getCustomerName()
                                       + " & ");
                    System.out.println(
                        c.getAccountNumber());
                }
 
                // Closing the connection to release memory
                // resources using close() method
                ois.close();
                fis.close();
 
                // Once all connection are closed after the
                // desired action change the flag state
                status = true;
            }
 
            // Catch block to handle the exception
            catch (Exception e) {
 
                // Print the exception on the console
                // along with display message
                System.out.println("Error Occurred" + e);
 
                // Exception encountered line is also
                // displayed on console using the
                // printStackTrace() method
                e.printStackTrace();
            }
        }
        return status;
    }
 
    // Method 2
    // To add a new customer
    public static boolean AddNewCustomer(Customer c)
    {
        // again, setting and initializing the flag boolean
        // value
        boolean status = false;
 
        // If customer is not present
        if (c != null) {
            // try block to check for exception
            try {
 
                // Initially assigning the object null to
                // avoid GC involvement
                FileOutputStream fos = null;
 
                // Creating an new FileOutputStream object
                fos = new FileOutputStream(
                    "BankAccountt.txt", true);
 
                // If there is nothing to be write onto file
                if (f.length() == 0) {
                    ObjectOutputStream oos
                        = new ObjectOutputStream(fos);
                    oos.writeObject(c);
                    oos.close();
                }
 
                // There is content in file to be write on
                else {
 
                    MyObjectOutputStream oos = null;
                    oos = new MyObjectOutputStream(fos);
                    oos.writeObject(c);
 
                    // Closing the FileOutputStream object
                    // to release memory resources
                    oos.close();
                }
 
                // Closing the File class object to avoid
                // read-write
                fos.close();
            }
 
            // Catch block to handle the exceptions
            catch (Exception e) {
 
                // Print the exception along with the
                // display message
                System.out.println("Error Occurred" + e);
            }
 
            // Change the flag status
            status = true;
        }
 
        return status;
    }
}


For now, save this code in a file CustomerCollection.java

Example 2: Customer.java  

Java




// Java program of Customer.java
import java.io.*;
 
class Customer implements Serializable {
    // Private class variables
    private String name;
    private long acc_No;
 
    // Class Constructor
    Customer(String n, long id)
    {
        acc_No = id;
        name = n;
    }
 
    // Getter methods of class variables
    public String getCustomerName() { return name; }
    public long getAccountNumber() { return acc_No; }
}


 
For now, save this code in a file Customer.java

Example 3: Main.java 

Java




// Java Program to illustrate Main.java
 
// Importing input output classes
import java.io.*;
 
// Main class
// Here all above helper classes comes into play
public class Main {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Class objects assigned with constructors
        // Customer input entries
        Customer c1 = new Customer("Rita", 1);
        Customer c2 = new Customer("Sita", 2);
 
        // Adding new customers as created above
        CustomerCollection.AddNewCustomer(c1);
        CustomerCollection.AddNewCustomer(c2);
 
        // Display message for better readability and
        // understanding
        System.out.println("****Reading File****");
 
        // Lastly reading File
        CustomerCollection.readFile();
    }
}


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

Output explanation:

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.

 



Last Updated : 08 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads