如何反序列化文件中的多個對象?以下是我嘗試過的代碼,它可以很好地適用於一個對象,但不適用於多個對象。如何從文件中反序列化多個對象
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)
嘗試,但沒有奏效。我需要做什麼改變?
爲什麼這似乎是一個考試問題,您發佈的代碼是該問題的默認主體? –
這不是一個考試問題。我正在申請工作和嘗試代碼,以便它可以幫助我進行面試。 – S1234
您正在關閉基礎流:'in.close(); fileIn.close();'。嘗試移動「while」循環之外的內容,看看它是如何發生的。 – npinti