This is a method to read show details from a .ser file containing serialized objects of type Show. The method successfully return the list but gives an Exception before. Why is it so and how do I get rid of it?
public List<Show> populateDataFromFile(String fileName) {
List<Show> shows=new ArrayList<Show>();
ObjectInputStream obj=null;
try {
FileInputStream fin=new FileInputStream(fileName);
obj=new ObjectInputStream(fin);
Show show=null;
while((show=(Show) obj.readObject())!=null)
{
shows.add(show);
show.getShowName();
}
System.out.println(shows);
} catch (IOException e) {
e.printStackTrace();
}catch(ClassNotFoundException e)
{
e.printStackTrace();
}finally
{
try {
obj.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return shows;
}
The output is
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2571)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1315)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at com.util.DataManagerImpl.populateDataFromFile(DataManagerImpl.java:23)
at com.psl.Client.main(Client.java:9)
Show Name: Sahi re Sahi
Show Time: 6:30 PM
Seats Available: 40
Show Name: Ek Shyam Aapke Naam
Show Time: 6:30 PM
Seats Available: 40
Show Name: Moruchi Maushi
Show Time: 6:30 PM
Seats Available: 40
Show Name: All The Best
Show Time: 6:30 PM
Seats Available: 40
Show Name: Naksharanche Dene
Show Time: 6:30 PM
Seats Available: 40
The main method is
public static void main(String[] args) {
DataManager dm=new DataManagerImpl();
List<Show>shows=dm.populateDataFromFile("ShowDetails.ser");
// Call all the functionalities from here to test your code.
for(Show show:shows)
{
System.out.println("Show Name: "+show.getShowName());
System.out.println("Show Time: "+show.getShowTime());
System.out.println("Seats Available: "+show.getSeatsAvailable());
}
}