public class SerializeDemo {
public static void serialize(Person person,String fileName) throws IOException {
new ObjectOutputStream(new FileOutputStream(fileName)).writeObject(person);
}
public static Person deSerialize(String fileName) throws IOException,ClassNotFoundException{
return (Person) new ObjectInputStream(new FileInputStream(fileName)).readObject();
}
public static void main(String[] args) throws IOException,ClassNotFoundException {
serialize(new Person("LordYi",22),"myText.byte");
System.out.println(deSerialize("myText.byte"));
}
}
public class Person implements Serializable {
private static final long serialVersionUID=2L;//重点来了哦
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
static ObjectStreamClass lookup(Class<?> cl, boolean all) {
if (!(all || Serializable.class.isAssignableFrom(cl))) {
return null;
}
return Caches.localDescs.get(cl);
}
private static class Caches {
/** cache mapping local classes -> descriptors */
static final ClassCache<ObjectStreamClass> localDescs =
new ClassCache<>() {
@Override
protected ObjectStreamClass computeValue(Class<?> type) {
return new ObjectStreamClass(type);
}
};
}
public long getSerialVersionUID() {
// REMIND: synchronize instead of relying on volatile?
if (suid == null) {
if (isRecord)
return 0L;
suid = AccessController.doPrivileged(
new PrivilegedAction<Long>() {
public Long run() {
return computeDefaultSUID(cl);
}
}
);
}
return suid.longValue();
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。