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