0

I am trying to build a program in Java involving serialization and deserialization. There are two classes, Employee and LauncherClass.

Employee.java:

import java.io.Serializable;

public class Employee implements Serializable {

    String name;
    int id;
    int age;
    double salary;

    public Employee() {
        this.name="";
        this.id=0;
        this.age=0;
        this.salary=0.0;
    }

    public void setName(String name) {
        this.name = name;

    }

    public String getName() {
        return name;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    public int getId()
    {
        return id;
    }

    public void setAge(int age)
    {
        this.age = age;
    }

    public int getAge()
    {
        return age;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public double getSalary()
    {
        return salary;
    }

    public String toString()
    {
        return id + " " + name + " " + age + " " + salary;
    }
}

LauncherClass.java:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Scanner;

public class LauncherClass {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Scanner sc = new Scanner(System.in);
        Employee employee = new Employee();
        FileOutputStream fileOutputStream = new FileOutputStream("datafile");
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
        int choice;
        do {
            System.out.println("Main Menu");
            System.out.println("1.Add an Employee");
            System.out.println("2.Display All");
            System.out.println("3.Exit");
            choice = sc.nextInt();
            switch(choice)
            {
            case 1:
                System.out.print("Enter Employee ID:");
                employee.setId(sc.nextInt());
                sc.nextLine();
                System.out.print("Enter Employee Name:");
                employee.setName(sc.nextLine());
                System.out.print("Enter Employee Age:");
                employee.setAge(sc.nextInt());
                System.out.print("Enter Employee Salary:");
                employee.setSalary(sc.nextDouble());        
                objectOutputStream.writeObject(employee);

                break;
            case 2:
                System.out.println("----Report----");
                FileInputStream fileInputStream = new FileInputStream("datafile");
                ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
                while(fileInputStream.available()>0)
                {
                employee = (Employee)objectInputStream.readObject();
                System.out.println(employee);
                }
                System.out.println("----End of Report---");
                objectInputStream.close();
                break;
            case 3:
                System.exit(5);


            }


        } while (choice!=3);
        objectOutputStream.flush();
        objectOutputStream.close();



    }

}

I execute this program and add some employees, then display the data of the object. Until this point, everything works fine. But when I exit and relaunch the program and select the display option, it shows nothing. Why is this so? Why is the data in the object being lost?

Boann
  • 48,794
  • 16
  • 117
  • 146
Javed
  • 1
  • 2

1 Answers1

0

There are a lot of problems with this.

  1. You're opening an ObjectOutputStream and before closing you are opening an ObjectInputStream. You need to decouple your reading and writing.

  2. Your while loop will never read the Closeable close/flush calls because of System.exit.

  3. You need to use System.out.println after using nextInt() because Scanner.nextInt() doesn't consume a newline.

  4. You need to wrap your Closeable objects in try-with-resources.

  5. The other user is correct, you need to append, not overwrite.

  6. You need to call ObjectOutputStream#flush after write.

   public static void main(String[] args) throws IOException, ClassNotFoundException {
        Scanner sc = new Scanner(System.in);

        Employee employee = new Employee();

        int choice;

        do {
            System.out.println("Main Menu");
            System.out.println("1.Add an Employee");
            System.out.println("2.Display All");
            System.out.println("3.Exit");

            choice = sc.nextInt();

            switch (choice) {
                case 1:
                    try (FileOutputStream fileOutputStream = new FileOutputStream("datafile", true);
                         ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream)) {
                        System.out.println("Enter Employee ID:");
                        employee.setId(sc.nextInt());
                        sc.nextLine();
                        System.out.println("Enter Employee Name:");
                        employee.setName(sc.nextLine());
                        System.out.println("Enter Employee Age:");
                        employee.setAge(sc.nextInt());
                        System.out.println("Enter Employee Salary:");
                        employee.setSalary(sc.nextDouble());
                        objectOutputStream.writeObject(employee);
                        objectOutputStream.flush();
                    }
                    break;

                case 2:
                    System.out.println("----Report----");
                    try (FileInputStream fileInputStream = new FileInputStream("datafile");
                         ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream)) {
                        System.out.println("as: " + objectInputStream.available());
                        while (fileInputStream.available() > 0) {
                            employee = (Employee) objectInputStream.readObject();
                            System.out.println(employee);
                        }
                        System.out.println("----End of Report---");
                    }
                    break;

                case 3:
                    break;
            }
        } while (choice != 3);
    }
user207421
  • 305,947
  • 44
  • 307
  • 483
Jason
  • 5,154
  • 2
  • 12
  • 22
  • Actually, u helped me a lot but here the problem is that, after adding more than one employee, when I display the data of the object, then it shows only first employee and after that it throws exception.Here is the detailed exception....Exception in thread "main" java.io.StreamCorruptedException: invalid type code: AC at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1622) at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:430) at LauncherClass.main(LauncherClass.java:51) – Javed May 14 '20 at 15:07
  • Refer to this post; https://stackoverflow.com/a/3182290/4417924 – Jason May 14 '20 at 15:55
  • Sorry bro, but the same error continues... even now it is having more unexpected results. – Javed May 15 '20 at 10:48
  • Please first execute it, and then tell me about the problem – Javed May 15 '20 at 10:49