public class Person implements Serializable { //private static final long serialVersionUID = -6802256731316734610L; private int age; private String name; private String sex; // private String height;
public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } // public String getHeight() { // return height; // } // public void setHeight(String height) { // this.height = height; // }
public class deserialize { public static Person deserializePerson() throws FileNotFoundException, IOException, ClassNotFoundException { ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File("/Users/ics/Desktop/Person.txt"))); Person person = (Person)in.readObject(); System.out.println("反序列化成功"); in.close(); return person;
}
public static void main(String args[]) throws FileNotFoundException, ClassNotFoundException, IOException { Person person = deserializePerson(); System.out.println(person.getName()+person.getSex()+person.getAge());
//private static final long serialVersionUID = -6802256731316734610L; private int age; private String name; private String sex; private String height;
public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getHeight() { return height; } public void setHeight(String height) { this.height = height; }
}
这时候对文件反序列化的时候,会出现如下错误
1
Exception in thread "main" java.io.InvalidClassException: Serialize.Person; local class incompatible: stream classdesc serialVersionUID = -6802256731316734610, local class serialVersionUID = -1407662779786941705