Java IO流 ObjectOutputStream、ObjectInputStream的基本使用

            ObjectOutputStream、ObjectInputStream的使用

  ObjectOutputStream将Java对象的原始数据类型和图形写入OutputStream。可以使用ObjectInputStream读取(重构)对象。 可以通过使用流的文件来实现对象的持久存储如果流是网络套接字流,则可以在另一个主机上或另一个进程中重构对象,只有支持java.io.Serializable接口的对象才能写入流中。方法readObject用于从流中读取对象应使用Java的安全铸造来获得所需的类型。 在Java中,字符串和数组是对象,在序列化过程中被视为对象。 读取时需要将其转换为预期类型。

  ObjectInputStream反序列化先前使用ObjectOutputStream编写的原始数据和对象,ObjectOutputStream和ObjectInputStream可以分别为与FileOutputStream和FileInputStream一起使用的对象图提供持久性存储的应用程序。 ObjectInputStream用于恢复先前序列化的对象。 其他用途包括使用套接字流在主机之间传递对象,或者在远程通信系统中进行封送和解组参数和参数。ObjectInputStream确保从流中创建的图中的所有对象的类型与Java虚拟机中存在的类匹配。 根据需要使用标准机制加载类。只能从流中读取支持java.io.Serializable或java.io.Externalizable接口的对象。方法readObject用于从流中读取对象。 应使用Java的安全铸造来获得所需的类型。 在Java中,字符串和数组是对象,在序列化过程中被视为对象。 读取时,需要将其转换为预期类型。可以使用DataInput上的适当方法

从流中读取原始数据类型。

  以下例子是ObjectOutputStream和ObjectInputStream的简单使用。首先是通过对象输出流ObjectOutputStream在指定文件写入数据对象,然后再通过ObjectInputStream进行读取。FileInputStream的使用可参考https://www.cnblogs.com/jhtian/p/14110083.html

测试代码:

package com.tianjh.io;

import java.io.*;

/**
 * Created on 2020/12/10
 * $ObjectStreamDemo 类必须实现Serializable接口才可以被序列化
 * 否则会抛NotSerializableException异常
 *
 * @author tianjh
 */
public class ObjectStreamDemo implements Serializable {
    private final String name;
    private final int id;

    public ObjectStreamDemo(String name, int id) {
        this.name = name;
        this.id = id;
    }

    @Override
    public String toString() {
        return "ObjectStreamDemo{" +
                "name='" + name + '\'' +
                ", id=" + id +
                '}';
    }

    public static void main(String[] args) {
        try {
            // 指定文件
            String sourceFile = "D:/ObjectStream.txt";
            /*
             * An ObjectOutputStream writes primitive data types and graphs of Java objects
             * to an OutputStream.  The objects can be read (reconstituted) using an
             * ObjectInputStream.  Persistent storage of objects can be accomplished by
             * using a file for the stream.  If the stream is a network socket stream, the
             * objects can be reconstituted on another host or in another process.
             *
             * ObjectOutputStream将Java对象的原始数据类型和图形写入OutputStream。
             * 可以使用ObjectInputStream读取(重构)对象。 可以通过使用流的文件来实现对象的持久存储
             * 如果流是网络套接字流,则可以在另一个主机上或另一个进程中重构对象,只有支持java.io.Serializable
             * 接口的对象才能写入流中
             */
            FileOutputStream fos = new FileOutputStream(sourceFile);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            // 将指定的对象写入ObjectOutputStream
            oos.writeObject(new ObjectStreamDemo("小明", 1));
            oos.writeObject(new ObjectStreamDemo("小王", 2));
            // 关闭相关流
            oos.close();
            fos.close();

            /*
             * An ObjectInputStream deserializes primitive data and objects previously
             * written using an ObjectOutputStream.
             *
             * ObjectInputStream反序列化先前使用ObjectOutputStream编写的原始数据和对象
             */
            FileInputStream fis = new FileInputStream(sourceFile);
            ObjectInputStream ois = new ObjectInputStream(fis);
            // 从ObjectInputStream读取一个对象
            ObjectStreamDemo e1 = (ObjectStreamDemo) ois.readObject();
            ObjectStreamDemo e2 = (ObjectStreamDemo) ois.readObject();

            System.out.println(e1.toString());
            // expected output: ObjectStreamDemo{name='小明', id=1}
            System.out.println(e2.toString());
            // expected output: ObjectStreamDemo{name='小王', id=2}
            // 关闭相关流
            ois.close();
            fis.close();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

输出结果:

 

posted @ 2020-12-10 20:36  tianjh  阅读(9308)  评论(0编辑  收藏  举报