读取存储Show对象的ArrayList文件时触发ClassCastException问题求助
Hey Willem, let's break down why you're hitting that ClassCastException and fix it step by step.
The Root Cause
Looking at your code, the core issue is a mismatch between what you're writing to the file and what you're trying to read:
- Your
writeShowDatamethod writes a singleShowobject to the file usingoos.writeObject(show); - But your
readShowDatamethod tries to cast the read object directly to aList<Show>withList <Show> shows= (List<Show>)ois.readObject();
Since the file contains a single Show instance (not a list), the JVM can't cast it to a List—hence the error: "class Show cannot be cast to class java.util.List".
Fix Options Based on Your Needs
Option 1: If You Only Need to Store/Read a Single Show
Modify the readShowData method to cast to a single Show instead of a list, and make sure you're reading the same file path you wrote to:
public void readShowData(String fileName) throws FileNotFoundException { // Match the full path from your write method to avoid reading the wrong file String showPath = "c:\\Users\\COMPUTER\\bluej files\\projects\\chapter15\\willem\\CinemaReservationSystemFiles\\"; // Use try-with-resources to auto-close streams (safer than manual close) try (FileInputStream fis = new FileInputStream(showPath + fileName); ObjectInputStream ois = new ObjectInputStream(fis)) { // Cast to Show instead of List<Show> Show show = (Show) ois.readObject(); System.out.println(show.toString()); // Test output } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } }
Option 2: If You Need to Store/Read Multiple Shows (a List)
If your goal is to save a collection of Show objects, adjust both the write and read methods to handle a List<Show>:
- Updated Write Method (writes the entire list instead of a single Show):
public void writeShowListData(String fileName, List<Show> shows) { String showPath = "c:\\Users\\COMPUTER\\bluej files\\projects\\chapter15\\willem\\CinemaReservationSystemFiles\\"; try (FileOutputStream fos = new FileOutputStream(showPath + fileName); ObjectOutputStream oos = new ObjectOutputStream(fos)) { oos.writeObject(shows); // Write the entire list to the file oos.flush(); } catch (IOException e) { e.printStackTrace(); } }
Important note: I removed the true (append) flag from FileOutputStream—appending with ObjectOutputStream breaks the file format because each write adds a new serialization header. If you need to add shows to an existing list, read the current list first, add the new show, then write the updated list back.
- Updated Read Method (reads the list correctly):
public void readShowListData(String fileName) { String showPath = "c:\\Users\\COMPUTER\\bluej files\\projects\\chapter15\\willem\\CinemaReservationSystemFiles\\"; try (FileInputStream fis = new FileInputStream(showPath + fileName); ObjectInputStream ois = new ObjectInputStream(fis)) { List<Show> shows = (List<Show>) ois.readObject(); System.out.println(shows.toString()); // Test output } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } }
Additional Tips
- Clear Corrupted Files: If you've been appending single
Showobjects toshows.ser, that file is now corrupted. Delete it before testing the fixed code. - Consistent File Paths: Always ensure your read and write methods use the same full file path to avoid reading from/writing to the wrong location.
- Try-With-Resources: This syntax automatically closes streams, preventing resource leaks and simplifying your code.
内容的提问来源于stack exchange,提问作者Willem

