首页 文章

使用ObjectInputStream从文件中读取对象? ClassNotFoundException的

提问于
浏览
0

在我程序的先前代码中,我使用ObjectOutputStream和FileOutPutStream在一个文件中保存了一个ArrayList(由我的代码中可以看到的名为location的自定义类的对象组成) . 但是,当尝试使用ObjectInputStream从文件中检索对象时,我收到一条错误消息,指出我有一个未处理的异常(ClassNotFoundException) .

这是我用来从文件中获取ArrayList的代码:

String file = "file";

ObjectInputStream input = new ObjectInputStream(new FileInputStream("file"));



ArrayList<location> arrayList = new ArrayList<location>();
arrayList = (ArrayList) input.readObject();

错误在我调用.readObject()方法的行上 . 任何帮助将不胜感激,因为我不熟悉Java . 谢谢!

1 回答

  • 0

    这意味着您发送的课程无法在您的应用中找到 . 您必须将其添加到应用程序的类路径,或者只发送应用程序具有的类 . 在您的情况下,缺少的类将在ArrayList中,因为ArrayList将始终存在 .

    没有什么是神秘的,错误意味着它所说的 .

    如果异常告诉你哪个类丢失了会更有用 . 我认为Java 7现在就这样做了 .

相关问题