Java 對象序列化

2018-02-04 17:19 更新

Java IO教程 - Java對象序列化


ObjectOutputStream類的一個對象用于序列化一個對象。

ObjectInputStream類的一個對象用于反序列化一個對象。

ObjectOutputStream繼承自O(shè)utputStream。 ObjectInputStream繼承自InputStream。

類必須實現(xiàn)Serializable或Externalizable接口以便序列化或反序列化。

Serializable接口是一個標記接口。

如果我們想要一個Person類的對象被序列化,我們需要聲明Person類如下:

public class Person   implements Serializable  {
    
}

Java負責處理從/向流讀取/寫入Serializable對象的細節(jié)。我們只需要將對象寫入/讀取流到流類中的一個方法。

實現(xiàn)Externalizable接口使我們能夠更好地控制從流中讀取和寫入對象。

它繼承Serializable接口。它聲明如下:

public interface  Externalizable extends Serializable  {
    void  readExternal(ObjectInput in)  throws   IOException,  ClassNotFoundException;
    void  writeExternal(ObjectOutput out) throws   IOException;
}

當我們從流中讀取一個對象時,Java調(diào)用readExternal()方法。當我們向一個流寫一個對象時,它調(diào)用writeExternal()方法。

我們必須編寫邏輯來分別讀取和寫入readExternal()和writeExternal()方法中的對象的字段。

實現(xiàn)Externalizable接口的類如下所示:

public class Person  implements Externalizable  {
    public void  readExternal(ObjectInput in)  throws   IOException,  ClassNotFoundException {
        // Write the logic to read the Person object fields  from  the   stream
    }    
    public void  writeExternal(ObjectOutput out) throws   IOException  {
        // Write  the   logic to write Person   object fields  to the   stream
    }
}

序列化對象

以下代碼創(chuàng)建ObjectOutputStream類的對象,并將對象保存到person.ser文件。

ObjectOutputStream oos  = new ObjectOutputStream(new FileOutputStream("person.ser"));

要將對象保存到ByteArrayOutputStream,我們構(gòu)造一個對象輸出流如下:

ByteArrayOutputStream baos  = new ByteArrayOutputStream();

// Creates an  object output stream to write objects to the   byte   array  output stream
ObjectOutputStream oos  = new ObjectOutputStream(baos);

使用ObjectOutputStream類的writeObject()方法通過將對象引用作為參數(shù)傳遞來序列化對象,如下所示:

oos.writeObject(p1);

最后,當我們完成將所有對象寫入時,使用close()方法關(guān)閉對象輸出流:

oos.close();

以下代碼顯示如何序列化實現(xiàn)可序列化接口的Person類。

import java.io.Serializable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

class Person implements Serializable {
  private String name = "Unknown";
  private String gender = "Unknown";
  private double height = Double.NaN;

  public Person(String name, String gender, double height) {
    this.name = name;
    this.gender = gender;
    this.height = height;
  }

  @Override
  public String toString() {
    return "Name: " + this.name + ", Gender:   " + this.gender + ",  Height: "
        + this.height;
  }
}

public class Main {
  public static void main(String[] args) {
    Person p1 = new Person("John", "Male", 1.7);
    Person p2 = new Person("Wally", "Male", 1.7);
    Person p3 = new Person("Katrina", "Female", 1.4);

    File fileObject = new File("person.ser");

    try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
        fileObject))) {

      oos.writeObject(p1);
      oos.writeObject(p2);
      oos.writeObject(p3);

      // Display the serialized objects on the standard output
      System.out.println(p1);
      System.out.println(p2);
      System.out.println(p3);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

上面的代碼生成以下結(jié)果。

反序列化對象

以下代碼顯示如何創(chuàng)建ObjectInputStream類的對象,并從person.ser文件讀取對象。

ObjectInputStream ois  = new ObjectInputStream(new FileInputStream("person.ser"));

要從ByteArrayInputStream讀取對象,請按如下所示創(chuàng)建對象輸出流:

ObjectInputStream ois  = new ObjectInputStream(Byte-Array-Input-Stream-Reference);

使用ObjectInputStream類的readObject()方法來反序列化對象。

Object obj  = oos.readObject();

最后,關(guān)閉對象輸入流如下:

ois.close();

以下代碼顯示如何從文件讀取對象。

import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;

public class Main {
  public static void main(String[] args) {
    File fileObject = new File("person.ser");

    try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
        fileObject))) {

      Person p1 = (Person) ois.readObject();
      Person p2 = (Person) ois.readObject();
      Person p3 = (Person) ois.readObject();

      System.out.println(p1);
      System.out.println(p2);
      System.out.println(p3);

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

可外化對象序列化

要序列化和反序列化可外部化對象,請實現(xiàn)Externalizable接口。

import java.io.Externalizable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

class PersonExt implements Externalizable {
  private String name = "Unknown";
  private String gender = "Unknown";
  private double height = Double.NaN;

  public PersonExt() {
  }

  public PersonExt(String name, String gender, double height) {
    this.name = name;
    this.gender = gender;
    this.height = height;
  }
  public String toString() {
    return "Name: " + this.name + ", Gender:   " + this.gender + ",  Height: "
        + this.height;
  }

  public void readExternal(ObjectInput in) throws IOException,
      ClassNotFoundException {
    this.name = in.readUTF();
    this.gender = in.readUTF();
  }

  public void writeExternal(ObjectOutput out) throws IOException {
    out.writeUTF(this.name);
    out.writeUTF(this.gender);
  }
}



public class Main {
  public static void main(String[] args) {
    PersonExt p1 = new PersonExt("John", "Male", 6.7);
    PersonExt p2 = new PersonExt("Wally", "Male", 5.7);
    PersonExt p3 = new PersonExt("Katrina", "Female", 5.4);

    File fileObject = new File("personext.ser");

    try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
        fileObject))) {
      oos.writeObject(p1);
      oos.writeObject(p2);
      oos.writeObject(p3);

      System.out.println(p1);
      System.out.println(p2);
      System.out.println(p3);
    } catch (IOException e1) {
      e1.printStackTrace();
    }

    fileObject = new File("personext.ser");

    try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
        fileObject))) {

      p1 = (PersonExt) ois.readObject();
      p2 = (PersonExt) ois.readObject();
      p3 = (PersonExt) ois.readObject();

      // Let"s display the objects that are read
      System.out.println(p1);
      System.out.println(p2);
      System.out.println(p3);

      // Print the input path
      System.out.println("Objects were  read   from  "
          + fileObject.getAbsolutePath());
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

上面的代碼生成以下結(jié)果。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號