从文件加载 ArrayList

发布于 2024-11-03 03:04:02 字数 823 浏览 2 评论 0原文

我刚刚将数组列表保存到文件中。现在我想将它加载回我的程序中,但它不起作用。这是我的代码:

 public void loadgame(){
    try {
        System.out.println("1");
        FileInputStream prein = new FileInputStream("saved game.txt");
        System.out.println("2");
        ObjectInputStream in = new ObjectInputStream(prein);
        System.out.println("3");
        kortene = (ArrayList<Kort>)in.readObject();
        System.out.println("4");
        in.close();

Iit 在 3 之后出现错误,错误是:

java.io.InvalidClassException: vendespil.Kort; local class incompatible: stream classdesc serialVersionUID = -8031146399228259151, local class serialVersionUID = -3342687010032791159

我的问题不是如何执行此操作,而是我已经按照其他人所说的做了,但它不起作用,我的问题是,为什么我会收到该错误,如果是因为我在保存和加载它之间更改了 ArrayList,我的意思是,我不更改文件,但在尝试加载它之前我确实清除了 ArrayList。

I've just saved an arraylist to a file. Now I want to load it back into my program, and it doesnt work. here is my code for it:

 public void loadgame(){
    try {
        System.out.println("1");
        FileInputStream prein = new FileInputStream("saved game.txt");
        System.out.println("2");
        ObjectInputStream in = new ObjectInputStream(prein);
        System.out.println("3");
        kortene = (ArrayList<Kort>)in.readObject();
        System.out.println("4");
        in.close();

Iit gets an error after 3, and the error is:

java.io.InvalidClassException: vendespil.Kort; local class incompatible: stream classdesc serialVersionUID = -8031146399228259151, local class serialVersionUID = -3342687010032791159

my problem isn't how to do this, but that i've done just what other people have said, and yet it doesn't work, my question is, why do i get that error, and if its because i change the ArrayList between saving it and loading it, i mean, i don't change the file, but i do clear the ArrayList before trying to load it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

停顿的约定 2024-11-10 03:04:02

看起来您在保存对象和读回对象之间对保存的类进行了更改。

我想说,再次清理并构建您的项目,然后尝试使用新的保存操作。在写作和阅读之间不要改变课堂上的任何内容

Looks like you have made changes to the saved class in between saving objects and reading them back.

I would say, clean and build your project again and try it with a newly save operation. Don't change anything in classes in between writing and reading

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文