Java - ObjectOutputStream.PutField Class



Description

The Java ObjectOutputStream.PutField class provide programmatic access to the persistent fields to be written to ObjectOutput.

Class declaration

Following is the declaration for Java.io.ObjectOutputStream.PutField class −

public abstract static class ObjectOutputStream.PutField
   extends Object

Class constructors

Sr.No.Constructor & Description
1

ObjectOutputStream.PutField()

Single Constructor

Class methods

Sr.No.Method & Description
1abstract void put(String name, boolean val)

This method puts the value of the named boolean field into the persistent field.

2abstract void put(String name, byte val)

This method puts the value of the named byte field into the persistent field.

3abstract void put(String name, char val)

This method puts the value of the named char field into the persistent field.

4abstract void put(String name, double val)

This method puts the value of the named double field into the persistent field.

5abstract void put(String name, float val)

This method puts the value of the named float field into the persistent field.

6abstract void put(String name, int val)

This method puts the value of the named int field into the persistent field.

7abstract void put(String name, long val)

This method puts the value of the named long field into the persistent field.

8abstract void put(String name, Object val)

This method puts the value of the named Object field into the persistent field.

9abstract void put(String name, short val)

This method puts the value of the named short field into the persistent field.

Methods inherited

This class inherits methods from the following classes −

  • Java.io.Object

Example - Serialize a User object with a custom boolean field

The following example shows the usage of ObjectOutputStream.PutField put(String name, boolean val) method. We're serializing only the isActive field explicitly, along with the name.

ObjectOutputStreamDemo.java

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;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) throws IOException, ClassNotFoundException {
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("user_bool1.ser"))) {
         User user = new User("Alice", true);
         oos.writeObject(user);

         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("user_bool1.ser"));
         User u = (User)ois.readObject();
         System.out.println(" User:: name= " + u.name + " isActive= "  + u.isActive);
      }
   }

   static class User implements Serializable {
      private static final long serialVersionUID = 1L;

      String name;
      boolean isActive;

      public User(String name, boolean isActive) {
         this.name = name;
         this.isActive = isActive;
      }

      private void writeObject(ObjectOutputStream oos) throws IOException {
         ObjectOutputStream.PutField fields = oos.putFields();
         fields.put("name", name);
         fields.put("isActive", isActive); // write boolean field manually
         oos.writeFields();
      }

      private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
         ObjectInputStream.GetField fields = ois.readFields();
         name = (String) fields.get("name", "Unknown");
         isActive = fields.get("isActive", false);
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

User:: name= Alice isActive= true

Explanation

  • The put() call explicitly writes the isActive boolean field.

  • On deserialization, the value is restored properly.

Example - Serialize a Student object with a grade (char)

The following example shows the usage of ObjectOutputStream.PutField put(String name, char val) method. We're writing a student's grade using put() method.

ObjectOutputStreamDemo.java

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;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) throws IOException, ClassNotFoundException {
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student1.ser"))) {
         Student s = new Student("Alice", 'A');
         oos.writeObject(s);

         ObjectInputStream ois = new ObjectInputStream( new FileInputStream("student1.ser"));
         Student s1 = (Student)ois.readObject();
         System.out.println("Student::  name = '" + s1.name + "' , grade = " + s1.grade);
      }
   }

   static class Student implements Serializable {
      private static final long serialVersionUID = 1L;

      String name;
      char grade;

      public Student(String name, char grade) {
         this.name = name;
         this.grade = grade;
      }

      private void writeObject(ObjectOutputStream oos) throws IOException {
         ObjectOutputStream.PutField fields = oos.putFields();
         fields.put("name", name);
         fields.put("grade", grade); // putting a char field
         oos.writeFields();
      }

      private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
         ObjectInputStream.GetField fields = ois.readFields();
         name = (String) fields.get("name", "Unknown");
         grade = fields.get("grade", 'F');
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Student::  name = 'Alice' , grade = A

Explanation

  • The grade field is written manually as a char using put().

Example - Serialize a User with a manually set String email field

The following example shows the usage of ObjectOutputStream.PutField put(String name, Object val) method. We're manually writing a String object field using put() method.

ObjectOutputStreamDemo.java

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;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("user_obj1.ser"))) {
         User user = new User("[email protected]");
         oos.writeObject(user);
      }

      // Deserialize
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("user_obj1.ser"))) {
         User user = (User) ois.readObject();
         System.out.println("Deserialized email: " + user.email);
      } catch (ClassNotFoundException e) {
         e.printStackTrace();
      }
   }

   static class User implements Serializable {
      private static final long serialVersionUID = 1L;

      String email;

      public User(String email) {
         this.email = email;
      }

      private void writeObject(ObjectOutputStream oos) throws IOException {
         ObjectOutputStream.PutField fields = oos.putFields();
         fields.put("email", email);  // write object field (String)
         oos.writeFields();
      }

      private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
         ObjectInputStream.GetField fields = ois.readFields();
         email = (String) fields.get("email", "[email protected]");
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Deserialized email: [email protected]

Explanation

  • A String object (email) is written using put("email", email).

  • It's a basic use of put() with an object-type field.