对象反序列化流ObjectInputStream

ObjectInputStream反序列化先前使用ObjectOutputStream编写的原始数据和对象。
构造方法:ObjectInputStream(InputStream in) 创建从指定的InputStream读取的ObjectInputStream。 
readObject()从ObjectInputStream读取一个对象。 
public static void main(String[] args) throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("myFile\\oos.txt"));

        Object obj = ois.readObject();

        //向下转型
        Student s = (Student)obj;

        System.out.println(s.getName()+","+s.getAge());
     ois.close(); }

运行结果:

通过对象反序列化流就可以反序列化先前使用对象序列化编写的原始数据和对象。

猜你喜欢

转载自www.cnblogs.com/pxy-1999/p/12768797.html