如何调试Java序列化错误?

时间:2018-02-01 06:31:33

标签: java serialization

我第一次学习Java序列化,它说可以保存对象的状态'

所以我尝试制作一个简单的Java控制台游戏,这将创建一个新的玩家'并将其默认IQ设置为80.无论何时他们运行游戏(即运行主要功能),他们都会发现他们的智商比前一次增加了1。

这是我的代码:

IQIncreaser.java

package IQIcreaserGame;

import java.io.Serializable;

public class IQIncreaser implements Serializable {
    private int iq;



    public int getIq() {
        return iq;
    }

    public void setIq(int iq) {
        this.iq = iq;
    }

    @Override
    public String toString() {
        return "Your IQ is now: " + iq;
    }
}

Main.java

package IQIcreaserGame;

import java.io.*;

public class Main {

    public static void main(String[] args) {
        IQIncreaser bakra = new IQIncreaser();
        bakra.setIq(80);

        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:/saveIQ.ser"));
            oos.writeObject(bakra);
            oos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("Welcome to the IQ increaser!!!");
        System.out.println("Whenver you run this game your IQ will increase by 1!!");
        System.out.println("Just check it out");


        System.out.println("Your IQ at beginning was " +bakra.getIq() + ", come back for more");

        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:/saveIQ.ser"));
            IQIncreaser restoredAndIncreased = (IQIncreaser) ois.readObject();

            // Here I am increasing the IQ by one everytime the main runs
            restoredAndIncreased.setIq(restoredAndIncreased.getIq()+1);

            System.out.println("The increased IQ is " + restoredAndIncreased.getIq());
            ois.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

现在问题/混淆部分

每当我运行游戏时,即每次运行main()函数时,我都认为我会得到一个预期的输出,其中玩家的智商将增加1,因为它需要"之前的值"从.ser保存的文件中添加1。 像这样,第一个默认值是80 然后第二次运行:81 然后在第三轮:82 然后在第四轮:83 ..等等

但每次我的价值都会增加81。问题出在哪里?解决这个问题的最佳方法是什么?

3 个答案:

答案 0 :(得分:0)

更新bakra.setIq

   try {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:/saveIQ.ser"));
        IQIncreaser restoredAndIncreased = (IQIncreaser) ois.readObject();

        // Here I am increasing the IQ by one everytime the main runs
        restoredAndIncreased.setIq(restoredAndIncreased.getIq()+1);
        bakra.setIq(restoredAndIncreased.getIq()+1);
        System.out.println("The increased IQ is " + restoredAndIncreased.getIq());
        ois.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

答案 1 :(得分:0)

您最初运行writeObject()功能,每次保存80作为值。但是当你从代码的后半部分增加值时,你不会将它写入文件。只需增加,阅读和忽略。

事件尽管你在那里写了它,它将再次替换为80,因为在main方法的上半部分有以下代码。

        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:/saveIQ.ser"));
            oos.writeObject(bakra);
            oos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

<强>解决方案

您必须先将值设置为80并将其保存到文件中。 然后,当你运行程序时,只需增加值并再次保存。不要再将它初始化为80.

 public static void main(String[] args) {
        IQIncreaser bakra = new IQIncreaser();
        bakra.setIq(80);

        try {
            if (!Paths.get("D:/saveIQ.ser").toFile().exists()) {
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("/tmp/abc.ser"));
                oos.writeObject(bakra);
                oos.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("Welcome to the IQ increaser!!!");
        System.out.println("Whenver you run this game your IQ will increase by 1!!");
        System.out.println("Just check it out");


        System.out.println("Your IQ at beginning was " + bakra.getIq() + ", come back for more");

        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:/saveIQ.ser"));
            IQIncreaser restoredAndIncreased = (IQIncreaser) ois.readObject();

            // Here I am increasing the IQ by one everytime the main runs
            restoredAndIncreased.setIq(restoredAndIncreased.getIq() + 1);

            System.out.println("The increased IQ is " + restoredAndIncreased.getIq());
            ois.close();

            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:/saveIQ.ser"));
            oos.writeObject(restoredAndIncreased);
            oos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

答案 2 :(得分:0)

请在下面找到完整的代码。基本上,您需要确保在创建文件时覆盖文件,否则,您将IQ恢复到80。 最后确保在递增IQ后将对象序列化为相同的filw。

class IQIncreaser implements Serializable{
    int Iq ;

    public int getIq() {
        return Iq;
    }

    public void setIq(int iq) {
        Iq = iq;
    }

}


public class Main {

    public static void main(String[] args) {
        Path path = Paths.get("/home/akshayap/saveIQ.ser");
        //create default file only if it does not exist
        if (Files.notExists(path)) {
        IQIncreaser bakra = new IQIncreaser();
        bakra.setIq(80);

        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("/home/akshayap/saveIQ.ser"));
            oos.writeObject(bakra);
            oos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        }

        System.out.println("Welcome to the IQ increaser!!!");
        System.out.println("Whenver you run this game your IQ will increase by 1!!");
        System.out.println("Just check it out");


        IQIncreaser restoredAndIncreased=null;

        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("/home/akshayap/saveIQ.ser"));
            restoredAndIncreased = (IQIncreaser) ois.readObject();
            System.out.println("Your IQ at beginning was " +restoredAndIncreased.getIq() + ", come back for more");
            // Here I am increasing the IQ by one everytime the main runs
            restoredAndIncreased.setIq(restoredAndIncreased.getIq()+1);

            System.out.println("The increased IQ is " + restoredAndIncreased.getIq());
            ois.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            //serialize the object into the same file post increment
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("/home/akshayap/saveIQ.ser"));
            oos.writeObject(restoredAndIncreased);
            oos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        }
    }