Python 官方文档:入门教程 => 点击学习
目录基本概念序列化反序列化序列化和反序列化总结自定义序列化策略Externalizabletransient静态变量序列化ID破坏单例总结基本概念 Java中创建对象时,一旦程序终止
public class Person implements Serializable {
private String name;
private int age;
public Person() {
System.out.println("无参构造...");
}
public Person(String name, int age) {
this.name = name;
this.age = age;
System.out.println("有参构造...");
}
@Override
public String toString() {
return "Person{" +
"name='" + name + "\'" +
", age='" + age + "\'"
"}";
}
}
public class SerializableTest {
public static void main(String[] args) throws ioException, ClassNotFoundException {
Person person = new Person("Lily", 20);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Person.txt"));
oos.writeObject(person);
oos.close();
}
}
public class DeserializableTest {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Person.txt"));
Pesron person = (Person)ois.readObject(ois);
System.out.println(person);
}
}
public class CustomExternal implements Externalizable {
private String name;
private int code;
public CustomExternal() {
System.out.println("无参构造...");
}
public CustomExternal(String name, int code) {
this.name = name;
this.code = code;
System.out.println("有参构造...");
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
System.out.println("执行writeExternal()方法...");
out.writeObject(name);
out.writeInt(code);
}
@Override
public void readExternal(ObjectInput in) throws IOException,ClassNotFoundException {
System.out.println("执行readExternal()方法...");
name = (String) in.readObject();
code = in.readInt();
}
@Override
public String toString() {
return "类:" + name + code;
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
CustomExternal custom = new CustomeExternal("oxford", 666);
System.out.println(custom);
// 序列化
ObjectOutputStream out = new ObjectOutputStream(new FileInputStream("oxford.txt"));
System.out.println("序列化对象...");
out.writeObject(custom);
out.close();
// 反序列化
ObjectInputStream in = new ObjectInputStream(new FileInputStream("oxford.txt"));
System.out.println("反序列化对象...");
custom = (CustomExternal) in.readObject();
System.out.println(custom);
in.close();
}
}
有参构造...
类:oxford666
序列化对象...
执行writeExternal()方法...
反序列化对象...
无参构造...
执行readExternal()方法...
类:oxford666
public final Object readObject()
throws IOException, ClassNotFoundException
{
if (enableOverride) {
return readObjectOverride();
}
int outerHandle = passhandle;
try {
Object obj = readObject0(false);
handles.markDependency(outerHandle, passHandle);
ClassNotFoundException ex = handles.lookupException(passHandle);
if (ex != null) {
throw ex;
}
if (depth == 0) {
vlist.doCallbacks();
}
return obj;
} finally {
passHandle = outerHandle;
if (closed && depth == 0) {
clear();
}
}
}
private Object readObject0(boolean unshared) throws IOException {
boolean oldMode = bin.getBlockDataMode();
if (oldMode) {
int remain = bin.currentBlockRemaining();
if (remain > 0) {
throw new OptionalDataException(remain);
} else if (defaultDataEnd) {
throw new OptionalDataException(true);
}
bin.setBlockDataMode(false);
}
byte tc;
while ((tc = bin.peekByte()) == TC_RESET) {
bin.readByte();
handleReset();
}
depth++;
totalObjectRefs++;
try {
switch (tc) {
case TC_NULL:
return readNull();
case TC_REFERENCE:
return readHandle(unshared);
case TC_CLASS:
return readClass(unshared);
case TC_CLASSDESC:
case TC_PROXYCLASSDESC:
return readClassDesc(unshared);
case TC_STRING:
case TC_LONGSTRING:
return checkResolve(readString(unshared));
case TC_ARRAY:
return checkResolve(readArray(unshared));
case TC_ENUM:
return checkResolve(readEnum(unshared));
case TC_OBJECT:
return checkResolve(readOrdinaryObject(unshared));
case TC_EXCEPTION:
IOException ex = readFatalException();
throw new WriteAbortedException("writing aborted", ex);
case TC_BLOCKDATA:
case TC_BLOCKDATALONG:
if (oldMode) {
bin.setBlockDataMode(true);
bin.peek();
throw new OptionalDataException(
bin.currentBlockRemaining());
} else {
throw new StreamCorruptedException(
"unexpected block data");
}
case TC_ENDBLOCKDATA:
if (oldMode) {
throw new OptionalDataException(true);
} else {
throw new StreamCorruptedException(
"unexpected end of block data");
}
default:
throw new StreamCorruptedException(
String.fORMat("invalid type code: %02X", tc));
}
} finally {
depth--;
bin.setBlockDataMode(oldMode);
}
}
private Object readOrdinaryObject(boolean unshared)
throws IOException
{
if (bin.readByte() != TC_OBJECT) {
throw new InternalError();
}
ObjectStreamClass desc = readClassDesc(false);
desc.checkDeserialize();
Class<?> cl = desc.forClass();
if (cl == String.class || cl == Class.class
|| cl == ObjectStreamClass.class) {
throw new InvalidClassException("invalid class descriptor");
}
Object obj;
try {
obj = desc.isInstantiable() ? desc.newInstance() : null;
} catch (Exception ex) {
throw (IOException) new InvalidClassException(
desc.forClass().getName(),
"unable to create instance").initCause(ex);
}
passHandle = handles.assign(unshared ? unsharedMarker : obj);
ClassNotFoundException resolveEx = desc.getResolveException();
if (resolveEx != null) {
handles.markException(passHandle, resolveEx);
}
if (desc.isExternalizable()) {
readExternalData((Externalizable) obj, desc);
} else {
readSerialData(obj, desc);
}
handles.finish(passHandle);
if (obj != null &&
handles.lookupException(passHandle) == null &&
desc.hasReadResolveMethod())
{
Object rep = desc.invokeReadResolve(obj);
if (unshared && rep.getClass().isArray()) {
rep = cloneArray(rep);
}
if (rep != obj) {
// Filter the replacement object
if (rep != null) {
if (rep.getClass().isArray()) {
filterCheck(rep.getClass(), Array.getLength(rep));
} else {
filterCheck(rep.getClass(), -1);
}
}
handles.setObject(passHandle, obj = rep);
}
}
return obj;
}
到此这篇关于Java基础入门之序列化和反序列化的文章就介绍到这了,更多相关Java序列化和反序列化内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Java基础入门总结之序列化和反序列化
本文链接: https://www.lsjlt.com/news/140680.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0