public List<Show> populateDataFromFile(String fileName)
{
List<Show> shows= null;
try{
ObjectInputStream in=new ObjectInputStream(new FileInputStream(fileName));
shows= (List<Show>) in.readObject();
in.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return shows;
}
I'm getting java.lang.ClassCastException: com.bean.Show cannot be cast to java.util.List
I've performed deserialization to initialise data of show class that is already serialized. I also make show class as serializable by implementing serializable i/f. I think readObject method
can't convert into ArrayList
.. Please tell me how do I convert it?
What you've written in the file, must be an instance of the Show class, not a List<Show>
. Perhaps, instead of outputStream.writeObject(list);
you did something like for(Show show: list) outputStream.writeObject(show);
? Try reading it back the same way:
List<Show> shows = new ArrayList<Show>();
try {
while(true) {
shows.add((Show) in.readObject());
} catch (EOFException e) {
in.close();
// end of file
}
显然,您已序列化的对象是Show
的单个对象,而不是您期望的Show
的List,请尝试将其强制转换为Show
或检查序列化,并确保您要序列化List<Show>
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.