You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

读取存储Show对象的ArrayList文件时触发ClassCastException问题求助

Fixing the ClassCastException in Your Cinema Reservation System's Show Data Handling

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 writeShowData method writes a single Show object to the file using oos.writeObject(show);
  • But your readShowData method tries to cast the read object directly to a List<Show> with List <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>:

  1. 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.

  1. 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 Show objects to shows.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 09:17:29