readobject()方法抛出ClassNotFoundException

时间:2019-05-27 21:48:46

标签: java serialization

我一直在跟着这段视频https://www.youtube.com/watch?v=YzwiuRDgSSY

我感觉好像和教程具有相同的代码,但是通过is.readObject()方法调用不断收到“ ClassNotFoundException”消息。

package com.jmoncayo;

import java.io.*;

public class Main {

    public static void main(String[] args) {
        Person arthur = new Person();
        arthur.name = "Arthur Dent";
        arthur.age = 25;

        String  filename = "data2.bin";
        try {
            ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(filename));
            os.writeObject(arthur);
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


        try {
            ObjectInputStream is = new ObjectInputStream(new FileInputStream(filename));
            Person p = (Person) is.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

package com.jmoncayo;

import java.io.Serializable;

public class Person implements Serializable {

    public String name;
    public int age;

}


0 个答案:

没有答案