2015-06-30 25 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

在這種情況下,解決辦法是:

  • 把所有的對象列表中的
  • 序列名單

這樣你只有一個對象去序列化:列表。 (作爲獎勵,你可以將你的對象放在一個組織良好(或不是!)列表中)。

如果您有多個需要序列化的對象類型,請將它們序列化爲每個類的列表。每個列表在不同的文件中。

相關問題