Satyaki
Satyaki

Reputation: 751

Deserialization In Java

I serialized a file with multiple objects. So I wrote a code like below -

FileOutputStream outputStream=new FileOutputStream(new File("Output.ser"));
ObjectOutputStream objectOutputStream=new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(employee);
objectOutputStream.writeObject(employee2);
objectOutputStream.writeObject(employee3);
objectOutputStream.writeObject(employee4);
objectOutputStream.close();

Now I want to de-serialize it, so to do that I took the help of WHILE loop to loop through all the objects ! The Code looks like below -

FileInputStream fileInputStream=new FileInputStream(new File(fileName));
ObjectInputStream stream=new ObjectInputStream(fileInputStream);
Employee emp;
while((emp=(Employee) stream.readObject())!=null)
{
    String uName=emp.getUserId();
    String uPass=emp.getPassword();
    map.put(uName, uPass);
}

This code making an EOFException reaching the end of file. I guess readObject() should return a null when it reaches the end of the file but in this case it ended up throwing an exception! Please do suggest me way(s) by which I can successfully read my serialized file without getting any exception!

Upvotes: 1

Views: 1951

Answers (3)

M..
M..

Reputation: 900

Your stream.readObject() will not return null when you reach the End of File. It will throw an EOFExcpetion to indicate that.

Your number of reads should basically match the number of writes. If you readObject in a loop you will face the EOFException.

Options you have are:

  1. Have as many readObject calls as writeObject calls by keeping a tab on number of objects written.

  2. Store in a List as @Richard has mentioned and call readObject once.

  3. In your posted implementation handle EOFException and do what you need to do when end of file has reached and close the stream.

Upvotes: 1

afzalex
afzalex

Reputation: 8652

I would prefer to use Collection Framework (e.g. ArrayList) or any other object which can encapsulate many objects when you want to serialize more than 1 objects.
But to work it your way, you have to give your serialized file a standard. You may add first object as an Integer which will tell you how much objects are stored in your file.

FileOutputStream outputStream=new FileOutputStream(new File("Output.ser"));
ObjectOutputStream objectOutputStream=new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(new Integer(4));
objectOutputStream.writeObject(employee);
objectOutputStream.writeObject(employee2);
objectOutputStream.writeObject(employee3);
objectOutputStream.writeObject(employee4);
objectOutputStream.close();

When desirializing, you can get the number of objects serialized.

FileInputStream fileInputStream=new FileInputStream(new File(fileName));
ObjectInputStream stream=new ObjectInputStream(fileInputStream);
Employee emp;
Integer size = (Integer) stream.readObject();
for(int i = 0; i < size; i++) {
    Employee emp = (Employee) stream.readObject();
    String uName=emp.getUserId();
    String uPass=emp.getPassword();
    map.put(uName, uPass);
}

It is a normal method how many files are stored in your filesystem but here you are doing it externally for your own purpose. Remember you will have to do necessary exception handling if you decide to work this way.

Upvotes: 1

user3838784
user3838784

Reputation:

Instead of serializing multiple objects, you should serialize a List like so :

FileOutputStream outputStream=new FileOutputStream(new File("Output.ser"));
ObjectOutputStream objectOutputStream=new ObjectOutputStream(outputStream);
ArrayList<Employee> employees = new ArrayList<Employee>();
employees.add(employee);
employees.add(employee2);
employees.add(employee3);
employees.add(employee4);
objectOutputStream.writeObject(employees);
objectOutputStream.close();

You can deserialize it like so :

FileInputStream fileInputStream=new FileInputStream(new File(fileName));
ObjectInputStream stream=new ObjectInputStream(fileInputStream);
for(Employee emp : (ArrayList)stream.readObject())
{
    String uName = emp.getUserId();
    String uPass = emp.getPassword();
    map.put(uName, uPass);
}

Upvotes: 3

Related Questions