package com.lxk.model; import java.io.Serializable; /** * @author lxk on 2017/11/1 */ public class FlyPig implements Serializable { //private static final long serialVersionUID = 1L; private static String AGE = "269"; private String name; private String color; transient private String car; //private String addTip; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getCar() { return car; } public void setCar(String car) { this.car = car; } //public String getAddTip() { // return addTip; //} // //public void setAddTip(String addTip) { // this.addTip = addTip; //} @Override public String toString() { return "FlyPig{" "name='" name '\'' ", color='" color '\'' ", car='" car '\'' ", AGE='" AGE '\'' //", addTip='" addTip '\'' '}'; } }
package com.lxk.test; import com.lxk.model.FlyPig; import java.io.*; /** * 序列化测试 * * @author lxk on 2017/11/1 */ public class SerializableTest { public static void main(String[] args) throws Exception { serializeFlyPig(); FlyPig flyPig = deserializeFlyPig(); System.out.println(flyPig.toString()); } /** * 序列化 */ private static void serializeFlyPig() throws IOException { FlyPig flyPig = new FlyPig(); flyPig.setColor("black"); flyPig.setName("naruto"); flyPig.setCar("0000"); // ObjectOutputStream 对象输出流,将 flyPig 对象存储到E盘的 flyPig.txt 文件中,完成对 flyPig 对象的序列化操作 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("d:/flyPig.txt"))); oos.writeObject(flyPig); System.out.println("FlyPig 对象序列化成功!"); oos.close(); } /** * 反序列化 */ private static FlyPig deserializeFlyPig() throws Exception { ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("d:/flyPig.txt"))); FlyPig person = (FlyPig) ois.readObject(); System.out.println("FlyPig 对象反序列化成功!"); return person; } }
public static void main(String[] args) throws Exception { serializeFlyPig(); //FlyPig flyPig = deserializeFlyPig(); //System.out.println(flyPig.toString()); }
InvalidClassException: com.lxk.model.FlyPig; local class incompatible: stream classdesc serialVersionUID = -3983502914954951240, local class serialVersionUID = 7565838717623951575