s-day10--反序列化流读取对象(ObjectInputStream)

This commit is contained in:
xx
2026-10-26 11:39:15 +08:00
parent 49591d0e10
commit dff891edf3
+26
View File
@@ -0,0 +1,26 @@
package com.inmind.xlh03;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
/*
15.反序列化流读取对象(ObjectInputStream)
构造方法:
ObjectInputStream(InputStream in) 创建从指定的InputStream读取的ObjectInputStream。
常用方法:
Object readObject() 从ObjectInputStream读取一个对象。
*/
public class Demo09 {
public static void main(String[] args) throws Exception {
//使用反序列化流,读取对象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("student.txt"));
Object o = ois.readObject();//多态
if (o instanceof Student) {
Student s = (Student) o;
System.out.println(s);
}
}
}