3

I am writing to a stream file in Java. I have read if I want to append I need to override WriteStreamHeader. But even after doing so, I am not able to get free from StreamCorruptedException.

class AppendingObjectOutputStream extends ObjectOutputStream {
      public AppendingObjectOutputStream(OutputStream out) throws IOException            {
        super(out);
      }
      protected void writeStreamHeader() throws IOException {
        reset();
      }
}
class Student implements Operations,Serializable
{
    private static final long serialVersionUID = 1L;
    private int studid;
    private String sname;
    private int sage;
    private long contact;
    public Student()
    {
        studid = 0;
        sname = null;
        sage = 0;
        contact = 0;
    }
    public void add(Scanner sc)
    {
        System.out.print("Enter student id: ");
        studid = Integer.parseInt(sc.nextLine());
        System.out.print("Enter student name: ");
        sname = sc.nextLine();
        System.out.print("Enter student age: ");
        sage = Integer.parseInt(sc.nextLine());
        System.out.print("Enter student's contact number: ");
        contact=Long.parseLong(sc.nextLine());
    }
    public void show()
    {
        System.out.println("Student's details:");
        System.out.println("Id no: "+studid);
        System.out.println("Name :" + sname);
        System.out.println("Age :" + sage);
        System.out.println("Contact No. :" + contact);
    }
}
class Admin
{
    public void addstu(Scanner sc)
    {
        try
        {
            Student s = new Student();
            s.add(sc);
            boolean b = true;
            FileInputStream fis = null;
            try{
                fis = new FileInputStream("student.ser");
                fis.close();
            }
            catch (FileNotFoundException e)
            {
                b = false;
            }
            FileOutputStream fos = new FileOutputStream("student.ser");
            ObjectOutputStream oos =null;
            if(b == true)
            {   
                System.out.println("Appending objects");
                oos = new AppendingObjectOutputStream(fos);
            }
            else
            {
                System.out.println("Writing objects");
                oos = new ObjectOutputStream(fos);
            }
            oos.writeObject(s);
            oos.close();
            fos.close();
            System.out.println("Student successfully inserted");
            fis = new FileInputStream("student.ser");
            ObjectInputStream ois = new ObjectInputStream(fis);
            Student result = (Student) ois.readObject();
            result.show();
            ois.close();
            fis.close(); 
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public void displayallstu() 
    {
        try
        {
            FileInputStream fis = new FileInputStream("student.ser");
            ObjectInputStream ois = new ObjectInputStream(fis);
            System.out.println("Student's List");
            try{
                while(true)
                {
                    Student result = (Student) ois.readObject();
                    result.show();
                }
            }
                 catch (EOFException e) {
                     System.out.println("!!End of file!!");
                    }
            finally{
            ois.close();
            fis.close();}
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } 
         catch (ClassNotFoundException e) {
                e.printStackTrace();
            } 
    }
}

It is called from main and executes properly when addstu() is called for the first time, but when called for next time, displays Successfully Inserted message, but throws exception while reading.

java.io.StreamCorruptedException: invalid stream header: 79737200
Nisha
  • 377
  • 1
  • 4
  • 13

1 Answers1

1

What you first generate is:

  • stream header
  • student

Then you are overwriting this with:

  • reset
  • student

which cannot be read since you’re missing the stream header. You want the following in the file:

  • stream header
  • student
  • reset
  • student

To get this, you will need to open the output stream in append mode after the file has been created the first time. Since you already have a Boolean indicating if this is necessary, just pass that to the constructor:

FileOutputStream fos = new FileOutputStream("student.ser", b);
AJNeufeld
  • 8,231
  • 1
  • 22
  • 40
  • If I am doing so, then my record is being appended. But after I call displayallstu(), it shows the very first record and then throws the exception `java.io.StreamCorruptedException: invalid type code: AC ` – Nisha Oct 31 '17 at 04:11
  • Interesting. This worked before, and you clearly are getting further now. There may have been more than one problem with your code, or ... perhaps something has changed? Are you using Java9 or an earlier version? I’ll continue looking at this tomorrow; or perhaps someone else will pick up the torch in the mean time. – AJNeufeld Oct 31 '17 at 04:28
  • Heartly thanks!!:) I checked my code again and found that after your suggestion, I removed changes for ObjectOutputStream. Now using appending for both ObjectOutputStream and FileOutputStream, I am able to do it!!Thanks again!! – Nisha Oct 31 '17 at 04:36