S1234
S1234

Reputation: 1

How to deserialize multiple objects from file

How do I deserialize multiple objects from a file? Following is code that I have tried which works fine for one object but not for multiple objects.

         public List<Show> populateDataFromFile(String fileName) {
        // TODO Auto-generated method stub
          Show s = null;
          //FileInputStream fileIn=null;

          try
          {
              FileInputStream fileIn=new FileInputStream("C:\\Users\\Admin\\Desktop\\Participant_Workspace\\Q1\\ShowBookingSystem\\ShowDetails.ser");  
                int i=0;  
                while((i=fileIn.read())!=-1){ 
          //   fileIn = new FileInputStream("C:\\Users\\Admin\\Desktop\\Participant_Workspace\\Q1\\ShowBookingSystem\\ShowDetails.ser");
             ObjectInputStream in = new ObjectInputStream(fileIn);

             s = (Show) in.readObject();
             in.close();
             fileIn.close();

              System.out.println("Name: " + s.getShowName());
              System.out.println("Show Time: " + s.getShowTime());
              System.out.println("Seats Available: " + s.getSeatsAvailable());
                }
          }catch(IOException i)
          {
             i.printStackTrace();

          }catch(ClassNotFoundException c)
          {
             System.out.println("Employee class not found");
             c.printStackTrace();

          }

        return null;
    }

I even tried using

while((i=fin.read())!=-1) 

but it did not work. What change do I need to make?

Upvotes: 0

Views: 2189

Answers (3)

Seb
Seb

Reputation: 1

In this case the solution is:

  • to put all objects in a list
  • serialize the list

This way you only have one object to de-serialize: the list. (As a bonus you get your object in a nicely organised (or not!) list).

If you have multiples type of object to serialize, serialize them in a list per class. Each list in a different file.

Upvotes: 0

npinti
npinti

Reputation: 52185

Below is a short working example. You will need to remove the ObjectInputStream in = new ObjectInputStream(fileIn); from outside the while loop as well.

    FileInputStream fis = new FileInputStream("...");        
    ObjectInputStream ois = new ObjectInputStream(fis);    //<- Outside the while loop.
    try            
    {
        while(true)
        {                
            Student std = (Student)ois.readObject();
            System.out.println(std.getName());
            System.out.println(std.getAge());
        }
    }
    catch(IOException e)   
    {
        e.printStackTrace();  //This exception will be thrown if the End Of File (EOF) is reached.
        //
    }
    finally
    {
          fis.close();    //<- Outside the while loop.
          ois.close();    //<- Outside the while loop.
    }

Upvotes: 0

Kachna
Kachna

Reputation: 2961

Try this way:

 Show s = null;
        try {
            FileInputStream fileIn = new FileInputStream(".....");
            ObjectInputStream in = new ObjectInputStream(fileIn);
            while (true) {
                try {
                    s = (Show) in.readObject();
                } catch (IOException ex) {
                   break;
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                }

               System.out.println("Name: " + s.getShowName());
              System.out.println("Show Time: " + s.getShowTime());
              System.out.println("Seats Available: " + s.getSeatsAvailable());
            }

            in.close();
            fileIn.close();

Upvotes: 1

Related Questions