从文件ClassCastException错误中读取对象

时间:2017-12-02 09:19:57

标签: java file serialization arraylist classcastexception

我正在尝试将一个名为Question的QuestionList对象写入文件,然后读取该文件。

我的问题是,当我读取文件时,它会给我一个错误,上面写着: java.lang.ClassCastException:java.lang.String无法强制转换为Quiz.load上的问题 < / p>

我的问题是,为什么会出现这个问题,我该如何解决?我一直在阅读很多教程,他们只是将对象转换为类名,这就是我所做的。我收录了我的保存和放大加载函数。

内部测验课程:

将对象写入文件

    ArrayList<Question> questions = new ArrayList<>();
    //filename given by user
    FileOutputStream fos = new FileOutputStream(filename);
    ObjectOutputStream oos = new ObjectOutputStream(fos);

    oos.writeObject(questions);

从文件中读取对象

    ArrayList<Question> readQuestions = new ArrayList<>();
    FileInputStream fis = new FileInputStream(filename);
    ObjectInputStream ois = new ObjectInputStream(fis);

    System.out.print("QUIZ LOADING...");

    readQuestions.add((Question)ois.readObject()); //problem occurs

3 个答案:

答案 0 :(得分:0)

您正在序列化列表并使用问题对其进行反序列化。

只需更改

  

readQuestions.add((Question)ois.readObject()); //出现问题

用这个

  

readQuestions = (ArrayList<Question>) ois.readObject();

进一步说明: 当我尝试这个例子时,我收到了这个错误: java.lang.ClassCastException:java.util.ArrayList无法强制转换为Question 因此,如果您使用String获取ClassCastException,则很可能还缺少Question上的Serializable接口。像这样:

class Question implements Serializable {
    String text;

    public Question(String text) {
        this.text = text;
    }
}

添加工作代码:

import java.io.*;
import java.util.ArrayList;

public class ObjectIS {
    public static void main(String[] args) {
        new ObjectIS().save();
        new ObjectIS().load("abcd");
    }

    public void save() {
        try {
            ArrayList<Question> questions = new ArrayList<>();
            questions.add(new Question("what is your name"));
            //filename given by user
            FileOutputStream fos = new FileOutputStream("abcd");
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            oos.writeObject(questions);

            oos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void load(String filename) {
        try {
            ArrayList<Question> readQuestions = new ArrayList<>();
            FileInputStream fis = new FileInputStream(filename);
            ObjectInputStream ois = new ObjectInputStream(fis);

            System.out.print("QUIZ LOADING...");

//            readQuestions.add((Question) ois.readObject()); //problem occurs
            readQuestions = (ArrayList<Question>) ois.readObject();
            System.out.println("ois = " + readQuestions);
            ois.close();
            fis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassCastException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
    }

}

class Question  implements Serializable {
    String text;

    public Question(String text) {
        this.text = text;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Question{");
        sb.append("text='").append(text).append('\'');
        sb.append('}');
        return sb.toString();
    }
}

答案 1 :(得分:0)

想象一下,你有一个空盒子。你把苹果放进了盒子里。

然后关闭它,稍后打开它。现在,你觉得在那个盒子里找到一个汉堡会有用吗?

但这就是你正在做的事情 - 存储一个String并期望找到一个Question对象。而那个阶级特殊的是jvm如何告诉你现实不适合你的假设。

解决方案:存储问题对象 - 或者在读取文件时期望字符串返回。

答案 2 :(得分:0)

正如我所说的那样。您正在序列化String

 oos.writeObject(questions.toString()); 

然后尝试将其反序列化为Question,它从未如此:

(Question)in.readObject();

解决方案:

  • 删除.toString()部分。
  • 反序列化为List<Question>,这就是它的真实含义。