开发者

Loading an ArrayList from a file

开发者 https://www.devze.com 2023-02-28 15:08 出处:网络
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:

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 t开发者_StackOverflowhe file, but i do clear the ArrayList before trying to load it.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消