0

i want to get multiple oblects of Employee class from the file using serialization. here i am getting only one object any solution??

    package com.jbt;

    import java.io.Serializable;

    public class Employee implements Serializable
    {
       public String firstName;
       public String lastName;
       private static final long serialVersionUID = 5462223600l;
    }




    package com.jbt;

    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;

    public class SerializaitonClass {

        public static void main(String[] args) {
            Employee emp = new Employee();
            emp.firstName = "Vivekanand";
            emp.lastName = "Gautam";

            try {
                FileOutputStream fileOut = new FileOutputStream("./employee.txt");
                ObjectOutputStream out = new ObjectOutputStream(fileOut);
                out.writeObject(emp);
                out.close();
                fileOut.close();
                System.out.printf("Serialized data is saved in ./employee.txt file");
            } catch (IOException i) {
                i.printStackTrace();
            }
        }
    }



package com.jbt;

import java.io.*;

public class DeserializationClass {
    public static void main(String[] args) {
        Employee emp = null;
        try {
            FileInputStream fileIn = new FileInputStream("./employee.txt");
            ObjectInputStream in = new ObjectInputStream(fileIn);
            emp = (Employee) in.readObject();
            in.close();
            fileIn.close();
        } catch (IOException i) {
            i.printStackTrace();
            return;
        } catch (ClassNotFoundException c) {
            System.out.println("Employee class not found");
            c.printStackTrace();
            return;
        }
        System.out.println("Deserializing Employee...");
        System.out.println("First Name of Employee: " + emp.firstName);
        System.out.println("Last Name of Employee: " + emp.lastName);
    }
}

here in Deserialization class how i can take multiple objects in List?? thanks in advance..!!

4
  • 2
    You're only writng one object. How can you possibly hope to read more than one? Commented Jul 10, 2015 at 11:23
  • @EJP this frogram was for only understanding we may suppose to insert more than one object. Commented Jul 10, 2015 at 11:54
  • 1
    If the program had really been 'for understanding' it would have shown Serialization of more than one object. There's no point in posting irrelevant code. People who know how to answer this question will already know how to serialize one or more objects. Commented Jul 11, 2015 at 0:52
  • @EJP some programmer may be new in java, u may be expert in this field so try to be more specific to the problem only rather than to find the mistakes in other's code you did not post any solution rather than saying code is "irrelevant","wrong" and And "using multiple ObjectInoutStreams is highly problematic too" if you find any solution then plz post that would be more appreciable. Commented Jul 11, 2015 at 4:10

2 Answers 2

1
List<Employee> employeeList = new ArrayList<Employee>();
FileInputStream fis = null;
ObjectInputStream in = null;
try {
    fis =new FileInputStream("./employee.txt");
    in = = new ObjectInputStream(fis);
    while (true) {
        employeeList.add((Employee) in.readObject());
    }
} catch (EOFException eof) {
    // Ignore. means you reached EOF
} finally {
    if (fis != null)
        fis.close(); // close the stream
    if(in != null)
        in.close();
}

NOTE : You can also serialize a List and store it on file and then deserialize it. (Added the list is a serializable List)

Sign up to request clarification or add additional context in comments.

10 Comments

I think as soon as it reach end of file it will throw EOFException.
since while(true) program is in never ending state. why you have declared ObjectInputStream in = new ObjectInputStream(fileIn); inside while block.
And using multiple ObjectInoutStreams is highly problematic too.
Edited, my bad that I initialized the stream inside while loop.
When you reach end of file and try to read the object it will throw an EOFException and hence we will come out of while loop then will finally close the streams. @PiyushMittal
|
1

You can serialize and de-serialize an array of objects as below:

File file = new File("out.ser");
        FileOutputStream fos = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fos);

        //Create the multiple Objects
        SerializeMe serializeMe = new SerializeMe(1);
        SerializeMe serializeMe1 = new SerializeMe(10);
        SerializeMe serializeMe2 = new SerializeMe(100);
        SerializeMe serializeMe3 = new SerializeMe(1000);

        //Create an array and assign objects
        Object[] obj=new Object[]{serializeMe,serializeMe1,serializeMe2,serializeMe3};
        // Write object array to Stream Class
        oos.writeObject(obj);
        oos.close();

        //Process of Deserializable
        FileInputStream fis = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(fis);

        //create an array of Object class
        Object[] obj1=(Object[]) ois.readObject();

        //looping the array
        for(int i=0;i<obj1.length;i++){

            SerializeMe dto = (SerializeMe) obj1[i];
            System.out.println("data : " + dto.getData());
        }
        ois.close();

1 Comment

thanks 4 ur reply but i am more specific to List. @Garry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.