进阶day10-.反序列化流读取对象(ObjectInputStream)
This commit is contained in:
30
javaSE-day10/src/com/inmind/object_stream03/Demo12.java
Normal file
30
javaSE-day10/src/com/inmind/object_stream03/Demo12.java
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package com.inmind.object_stream03;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
/*
|
||||||
|
反序列化流读取对象(ObjectInputStream)
|
||||||
|
|
||||||
|
构造方法:
|
||||||
|
ObjectInputStream(InputStream in) 创建从指定的InputStream读取的ObjectInputStream。
|
||||||
|
|
||||||
|
常用方法:
|
||||||
|
Object readObject() 从ObjectInputStream读取一个对象。
|
||||||
|
*/
|
||||||
|
public class Demo12 {
|
||||||
|
//将之前的student.txt读取到java进程中生成一个叫张三,18的Student对象
|
||||||
|
public static void main(String[] args) throws IOException, ClassNotFoundException {
|
||||||
|
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("student.txt"));
|
||||||
|
Object o = ois.readObject();
|
||||||
|
if (o instanceof Student) {
|
||||||
|
Student s = ((Student) o);
|
||||||
|
System.out.println(s.name);
|
||||||
|
System.out.println(s.age);
|
||||||
|
}
|
||||||
|
ois.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user