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

This commit is contained in:
2026-08-12 11:39:24 +08:00
parent cfd3bcb417
commit dd6bbc23bf
@@ -0,0 +1,25 @@
package com.inmind.object_stream03;
import java.io.*;
/*
反序列化流读取对象(ObjectInputStream)
构造方法:
ObjectInputStream(InputStream in) 创建从指定的InputStream读取的ObjectInputStream。
常用方法:
Object readObject() 从ObjectInputStream读取一个对象。
*/
public class Demo10 {
public static void main(String[] args) throws Exception {
//使用反序列化流读取之前保存的学生对象
FileInputStream fis = new FileInputStream("student.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Object o = ois.readObject();//多态:父类引用指向子类对象
if (o instanceof Student) {
Student s = (Student) o;
System.out.println(s);
}
}
}