writing and reading object in a binary file

101 Views Asked by At

I am making a program where user inputs his/her info and it is written in the file in binary form and after that we ask for a name whose info we want to get from the file. In that I take all the inserted objects into a arraylist and then match the name with respective name corresponding to the objects

this is my code

import java.util.*;

import java.io.*;

class MyObjectOutputStream extends ObjectOutputStream {

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

    @Override
    protected void writeStreamHeader() {}
}

interface IIITN {
    final String InstituteName = "IIIT Nagpur";
}

class Student implements Serializable
{
    String name;
    String RollNo;
    String branch;
    String gender;
    String Degree;

    Student(String name, String RollNo, String branch, String gender, String Degree) {

        this.name = name;
        this.RollNo = RollNo;
        this.branch = branch;
        this.gender = gender;
        this.Degree = Degree;
    }
}

class CSE implements IIITN {
    String name;

    public void input(File CSEStudent) throws IOException {
        Scanner sc = new Scanner(System.in);
        String name = sc.next();
        String RollNo = sc.next();
        String branch = sc.next();
        String gender = sc.next();
        String Degree = sc.next();
        Student s1 = new Student(name, RollNo, branch, gender, Degree);
        FileOutputStream fs = new FileOutputStream(CSEStudent,true);
        MyObjectOutputStream obj = new MyObjectOutputStream(fs);
        obj.writeObject(s1);
        // obj.close();
    }

    public void display(File f) throws IOException {
        FileInputStream CS = new FileInputStream(f);
        
        // ObjectInputStream obj = new ObjectInputStream(CS);
        // ArrayList<Student> objectList = new ArrayList<>();
        Student s = null;
        boolean isExist = false;
        ObjectInputStream obj = new ObjectInputStream(CS);
        while (!isExist) {
            try {
                // ObjectInputStream obj = new ObjectInputStream(CS);
                s = (Student) obj.readObject();
                if (s != null) {
                    System.out.println(s.name);
                }
            } catch (Exception e) {
                System.out.println();
                isExist = true;
               
            }
        }
    }

    public void setname(File f) throws IOException{
        System.out.println("Enter the name of student");
        Scanner sc = new Scanner(System.in);
        this.name = sc.next();
        ArrayList<Student> obj = new ArrayList<>();
        display(f);
    //     for (Student student : obj) {
    //         System.out.println(student.name);
    //     }
    //     System.out.println("Enter");
    //     for (int i = 0; i < obj.size(); i++) {
    //        if((obj.get(i)).name == this.name) {
    //         System.out.println((obj.get(i)).name);
    //         System.out.println((obj.get(i)).RollNo);
    //        } else {
    //         System.out.println("Not Found");
    //        }
    //    }
    }
}



public class Main {
    public static void main(String[] args) throws IOException{
        CSE c1 = new CSE();
        File f = new File("./CSE.txt");
        c1.input(f);
        c1.input(f);
        c1.setname(f);
    }
}

It is taking the info of the student and is writing it in the file but when it comes to read from the file it gives me this error

Exception in thread "main" java.io.StreamCorruptedException: invalid stream header: 73720007
        at java.base/java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:987)
        at java.base/java.io.ObjectInputStream.<init>(ObjectInputStream.java:414)
        at CSE.display(Main.java:61)
        at CSE.setname(Main.java:82)
        at Main.main(Main.java:187)
0

There are 0 best solutions below