对象操作流ObjectInputStream
-
读取:new ObjectInputStream(InputStream),readObject()
public static void main(String[] args) throws IOException, ClassNotFoundException { ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e.txt")); Person p1 = (Person) ois.readObject(); Person p2 = (Person) ois.readObject(); System.out.println(p1); System.out.println(p2); ois.close(); }
package com.heima.otherio;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import com.heima.bean.Person;
public class Demo05_ObjectInputStream {
/**
* @param args
* @throws IOException
* @throws ClassNotFoundException
* ObjectInputStream
* 对象输入流是反序列化
* 反序列化:将对象读出到内存中
*/
public static void main(String[] args) throws IOException,
ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e.txt"));
Person p1 = (Person) ois.readObject();
Person p2 = (Person) ois.readObject();
//当文件读取到了末尾时出现EOFException(end of file)
//Person p3 = (Person) ois.readObject();
System.out.println(p1);
System.out.println(p2);
ois.close();
}
}