Java 教程

Java 控制語句

面向物件程式設計

Java 內建類

Java 檔案處理

Java 錯誤與異常

Java 多執行緒

Java 同步

Java 網路程式設計

Java 集合

Java 介面

Java 資料結構

Java 集合演算法

高階Java

Java 其他

Java APIs與框架

Java 類引用

Java 有用資源

Java - transient關鍵字



在Java中,序列化是一個概念,我們可以使用它將物件的狀態寫入位元組流中,以便我們可以透過網路傳輸它(使用JPA和RMI等技術)。

序列化類的物件時,如果希望JVM忽略特定的例項變數,可以將其宣告為transient。

語法

public transient int limit = 55; // will not persist
public int b; // will persist

示例

在下面的Java程式中,Student類有兩個例項變數name和age,其中age被宣告為transient。在名為ExampleSerialize的另一個類中,我們嘗試序列化和反序列化Student物件並顯示其例項變數。由於age被設定為不可見(transient),因此只顯示name的值。

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Student implements Serializable{

   private static final long serialVersionUID = 1L;
   private String name;
   private transient int age;

   public Student(String name, int age){
      this.name = name;
      this.age = age;
   }
   public String getName() {
      return this.name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public void setAge(int age) {
      this.age = age;
   }
   public int getAge() {
      return this.age;
   }
}
public class ExampleSerialize{
   public static void main(String args[]) throws Exception{
      Student std1 = new Student("Krishna", 30);
      FileOutputStream fos = null;
      ObjectOutputStream oos = null;
      FileInputStream fis = null;
      ObjectInputStream ois = null;

      try {
         fos = new FileOutputStream("/tmp/student.ser");
         oos = new ObjectOutputStream(fos);
         oos.writeObject(std1);

         fis = new FileInputStream("/tmp/student.ser");
         ois = new ObjectInputStream(fis);
         Student std2 = (Student) ois.readObject();
         System.out.println(std2.getName());
      } catch(IOException e) {
         oos.close();
         fos.close();
         ois.close();
         fis.close();    	  
      }
   }
}

輸出

Krishna

示例

在下面的Java程式中,我們建立、序列化和反序列化一個employee物件。

員工

package com.tutorialspoint;

public class Employee implements java.io.Serializable {
   public String name;
   public String address;
   public transient int SSN;
   public int number;
   
   public void mailCheck() {
      System.out.println("Mailing a check to " + name + " " + address);
   }
}

SerializeDemo

package com.tutorialspoint;

import java.io.*;
public class SerializeDemo {

   public static void main(String [] args) {
      Employee e = new Employee();
      e.name = "Reyan Ali";
      e.address = "Phokka Kuan, Ambehta Peer";
      e.SSN = 11122333;
      e.number = 101;
      
      try {
         FileOutputStream fileOut =
         new FileOutputStream("/tmp/employee.ser");
         ObjectOutputStream out = new ObjectOutputStream(fileOut);
         out.writeObject(e);
         out.close();
         fileOut.close();
         System.out.printf("Serialized data is saved in /tmp/employee.ser");
      } catch (IOException i) {
         i.printStackTrace();
      }
   }
}

下面的SerializeDemo程式例項化一個Employee物件並將其序列化到檔案中。程式執行完畢後,將建立一個名為employee.ser的檔案。

DeserializeDemo

package com.tutorialspoint;

import java.io.*;
public class DeserializeDemo {

   public static void main(String [] args) {
      Employee e = null;
      try {
         FileInputStream fileIn = new FileInputStream("/tmp/employee.ser");
         ObjectInputStream in = new ObjectInputStream(fileIn);
         e = (Employee) in.readObject();
         in.close();
         fileIn.close();
      } catch (IOException i) {
         i.printStackTrace();
         return;
      } catch (ClassNotFoundException c) {
         System.out.println("Employee class not found");
         c.printStackTrace();
         return;
      }
      
      System.out.println("Deserialized Employee...");
      System.out.println("Name: " + e.name);
      System.out.println("Address: " + e.address);
      System.out.println("SSN: " + e.SSN);
      System.out.println("Number: " + e.number);
   }
}

輸出

Deserialized Employee...
Name: Reyan Ali
Address:Phokka Kuan, Ambehta Peer
SSN: 0
Number:101

以下是需要注意的重要幾點:

  • try/catch塊嘗試捕獲ClassNotFoundException,這是readObject()方法宣告的。為了使JVM能夠反序列化物件,它必須能夠找到該類的位元組碼。如果JVM在物件的反序列化過程中找不到類,則會丟擲ClassNotFoundException。

  • 請注意,readObject()的返回值被強制轉換為Employee引用。

  • 當物件被序列化時,SSN欄位的值為11122333,但由於該欄位是transient,因此此值未傳送到輸出流。反序列化的Employee物件的SSN欄位為0。

java_basic_syntax.htm
廣告
© . All rights reserved.