How does the serialisation using child class of ObjectOutputStream does not give error when reading the objects using ObjectInputStream?

22 views Asked by At

When serialising multiple time using ObjectOutputStream object it was setting the new header at the end of file each time. While reading the Object it was throwing the StreamCorruptedException . This problem was solved by making the child class of ObjectOutputStream and using child class Object to serialise data .

public class SerializeDataInAndOut {
    private File f=new File("studentFile.txt");
    public SerializeDataInAndOut()  {
        try {
            // Creating new file using File object above
            f.createNewFile();
        }

        // Catch block to handle the exception
        catch (Exception e) {
            System.out.println("Exception Occured!");
        }
    }

    public void addData(Student student)
    {
        FileOutputStream fileOutputStream=null;
        try{
            fileOutputStream=new FileOutputStream("studentFile.txt", true);
        }
        catch(FileNotFoundException ex){
            System.out.println("File Not Found!");
            ex.printStackTrace();
        }
        catch (Exception ex)
        {
            System.out.println("Exception Occured!");
        }

        if(f.length()==0) {
            try (ObjectOutputStream outputStream = new ObjectOutputStream(fileOutputStream)) {

                outputStream.writeObject(student);
            } catch (IOException ex) {
                System.out.println("Error occured while writing data in file");
                ex.printStackTrace();
            }
        }
        else{
            try (MyObjectOutputStream myOutputStream = new MyObjectOutputStream(fileOutputStream)) {
                myOutputStream.writeObject(student);
            } catch (IOException ex) {
                System.out.println("Error occured while writing data in file");
                ex.printStackTrace();
            }
        }
        try{
            fileOutputStream.close();
        }
        catch (IOException ex)
        {
            System.out.println("Input Output Exception Occured!");
            ex.printStackTrace();
        }

    }

    public List<Student> getData() {

        if(f.length()==0)
        {
            System.out.println("No Record Found!!!");
            return null;
        }
        List<Student> allStudents=new ArrayList<>();
        FileInputStream fileInputStream=null;
        try{
            fileInputStream= new FileInputStream("studentFile.txt");
        }
        catch(FileNotFoundException ex){
            System.out.println("File Not Found!");
            ex.printStackTrace();
        }
        catch (Exception ex) {
            System.out.println("Exception Occured!");
        }

        try(ObjectInputStream inputStream=new ObjectInputStream(fileInputStream)){

            System.out.println("Getting Object ");
            while(true) {
                try{
                    Student student=(Student) inputStream.readObject();
                    allStudents.add(student);
                }
                catch(EOFException ex){
                    break;
                }
            }
        }
        catch(IOException ex) {
            System.out.println("Exception Occured!");
            ex.printStackTrace();
        }
        catch (ClassNotFoundException ex) {
            System.out.println("Error Class not Found!");
            ex.printStackTrace();
        }
        return allStudents;
    }
}

Here is the child class of ObjectOutputStream

class MyObjectOutputStream extends ObjectOutputStream{

    public MyObjectOutputStream(OutputStream out) throws IOException {
        super(out);
    }

    public MyObjectOutputStream() throws IOException {
        super();
    }

    public void writeStreamHeader() throws IOException
    {
        return;
    }

}

My question here is that how serialising with child class object is not giving me an exception , here also we are creating the new Object of the child class of ObjectOutputStream , will not set the header each time?

0

There are 0 answers