从文件加载 ArrayList
我刚刚将数组列表保存到文件中。现在我想将它加载回我的程序中,但它不起作用。这是我的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您在保存对象和读回对象之间对保存的类进行了更改。
我想说,再次清理并构建您的项目,然后尝试使用新的保存操作。在写作和阅读之间不要改变课堂上的任何内容
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