2015-06-30 24 views
0

如何反序列化文件中的多个对象?以下是我尝试过的代码,它可以很好地适用于一个对象,但不适用于多个对象。如何从文件中反序列化多个对象

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

我甚至使用

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

尝试,但没有奏效。我需要做什么改变?

+1

为什么这似乎是一个考试问题,您发布的代码是该问题的默认主体? –

+0

这不是一个考试问题。我正在申请工作和尝试代码,以便它可以帮助我进行面试。 – S1234

+1

您正在关闭基础流:'in.close(); fileIn.close();'。尝试移动“while”循环之外的内容,看看它是如何发生的。 – npinti

回答

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

代码工作正常ie即它反序列化整个文件,但在结束异常弹出java.io.ObjectInputStream java.io.EOFException $ BlockDataInputStream.peekByte(未知源)在java.io.ObjectInputStream.readObject0(未知源)在java .io.ObjectInputStream.readObject(Unknown Source)at com.util.DataManagerImpl.populateDataFromFile(DataManagerImpl.java:43)at com.psl.Client.main(Client.java:12) – S1234

+0

即例行s =(Show) in.readObject(); – S1234

+0

曾为..谢谢你这么多 – S1234

0

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

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

代码运行良好即它deserialze整个文件,但在弹出java.io.EOFException的 \t在java.io.ObjectInputStream中的$ BlockDataInputStream.peekByte(来源不明)结束例外 \t是java .io.ObjectInputStream.readObject0(Unknown Source) \t at java.io.ObjectInputStream。readObject(Unknown Source) \t at com.util.DataManagerImpl.populateDataFromFile(DataManagerImpl.java:43) \t at com.psl.Client.main(Client.java:12) – S1234

+0

ie for line Student std =(Student)ois .readObject(); – S1234

+0

工作..非常感谢你 – S1234

0

在这种情况下,解决办法是:

  • 把所有的对象列表中的
  • 序列名单

这样你只有一个对象去序列化:列表。 (作为奖励,你可以将你的对象放在一个组织良好(或不是!)列表中)。

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

相关问题