0

Below is a program that should deserialize the record of employees. It does deserialize the first record but it does not deserialize the other records added by the user.

import java.io.*;
import java.util.*;

public class Employee implements Serializable {

    private Object name;
    private Object address;
    private Object ssn;
    private Object eadd;
    private Object number;  

    private void writeObject(ObjectOutputStream os) throws IOException {
        os.defaultWriteObject();
    }

    private void readObject (ObjectInputStream is) throws IOException, ClassNotFoundException {
        is.defaultReadObject();
    }


    public static void addEmployee() {
        try {

            Employee e = new Employee();
            BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
            FileOutputStream fileOut = new FileOutputStream("employee.ser", true);
            ObjectOutputStream out =  new ObjectOutputStream(fileOut);
            System.out.println("Name: ");
            e.name = buffer.readLine();
            System.out.println("Address: ");
            e.address = buffer.readLine();
            System.out.println("SSN: ");
            e.ssn = buffer.readLine();
            System.out.println("Email: ");
            e.eadd = buffer.readLine();
            System.out.println("Number: ");
            e.number = buffer.readLine();

            out.writeObject(e +"#");
            out.close();
            fileOut.close();

        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void deserializeAll() throws ClassNotFoundException {
        Employee e = null;
        try {
            FileInputStream fileIn = new FileInputStream("employee.ser");
            ObjectInputStream in = new ObjectInputStream(fileIn);
            e = (Employee) in.readObject();
            in.close();
            fileIn.close();
            System.out.println("Deserialized Employee...");
            System.out.println("Name: " + e.name);
            System.out.println("Address: " + e.address);
            System.out.println("SSN: " + e.ssn);
            System.out.println("Email: " + e.eadd);
            System.out.println("Number: " + e.number);
        }
        catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    public static void main(String[] args) throws ClassNotFoundException {
        int choice;
        Scanner input = new Scanner (System.in);    

        do {
            System.out.println("[1] Add an employee.");
            System.out.println("[2] Deserialize all.");
            choice = input.nextInt();
            switch (choice) {
                case 1: addEmployee(); break;
                case 2: deserializeAll(); break;
                default: System.exit(0);
            }
        } while (choice != 3);

    }

}

The # is the delimeter per record.

Michaël
  • 3,679
  • 7
  • 39
  • 64

1 Answers1

4

You can't append to an ObjectOutputStream. Once you have closed it you cannot add to it.

Instead you either need to re-write the entire file or use a different file format (one which allow appending of data)

BTW: This doesn't write the Object

out.writeObject(e +"#");

It writes the e.toString() + "#" which is a String when you deserialize it, not the original object.

I would make your field types String instead of Object.

And I would delete your writeObject and readObject methods as they don't do anything.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Thank you! How will I now print all the deserialized records? I'm sorry I'm still confused with Java. – Nelson Friese Sep 03 '12 at 14:21
  • First you have to write all the records to disk correctly. I suggest loading a `List` from the file, add your new record and write the list out again. Once you have done that you can generate a `toString()` method for Employee and it will display correctly. – Peter Lawrey Sep 03 '12 at 14:24
  • Sir, about the append thingy, can I use `DataOutputStream` and de/serialization will still work? – Nelson Friese Sep 04 '12 at 12:36
  • You can use DataOutputStream *instead of* Java Serialisation. If you want normal Java Serialisation, you have to use ObjectOutputStream and ObjectInputStream. – Peter Lawrey Sep 04 '12 at 13:12
  • I read [this](http://stackoverflow.com/questions/1194656/appending-to-an-objectoutputstream) and I'm wondering if it will help my code. Can you please guide me, sir, in implementing that in my code? :) – Nelson Friese Sep 04 '12 at 13:22
  • I don't believe that is the answer. Instead I still think the simplest solution is to read a List of your Employees and when you update, write out the List of Employees again. – Peter Lawrey Sep 04 '12 at 13:24