How to deserialize multiple objects from file

时间:2015-06-30 13:55:11

标签: java deserialization

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?

3 个答案:

答案 0 :(得分:0)

尝试这种方式:

 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();

答案 1 :(得分:0)

以下是一个简短的工作示例。您还需要从ObjectInputStream in = new ObjectInputStream(fileIn);循环之外移除while

    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.
    }

答案 2 :(得分:0)

在这种情况下,解决方案是:

  • 将所有对象放入列表
  • 序列化列表

这样你只有一个反序列化对象:列表。 (作为奖励,你可以在一个组织得很好(或没有!)的列表中得到你的对象)。

如果您有多个类型的对象要序列化,请在每个类的列表中序列化它们。每个列表都在不同的文件中。