对象反序列化流ObjectInputStream

Posted pxy-1999

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对象反序列化流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("myFileoos.txt"));

        Object obj = ois.readObject();

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

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

运行结果:

技术图片

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

技术图片

以上是关于对象反序列化流ObjectInputStream的主要内容,如果未能解决你的问题,请参考以下文章