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();
        }
    }
}

輸出結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM