Java - ObjectOutputStream.PutField put(String name, Object val) method



Description

The Java ObjectOutputStream.PutField put(String name, Object val) method puts the value of the named Object field into the persistent field.

  • It sets the value of an object-type field (like String, Integer, or custom classes) when using manual serialization via putFields().

  • Used inside a custom writeObject(ObjectOutputStream oos) method.

  • You must call writeFields() afterward to write the fields to the stream.

Declaration

Following is the declaration for java.io.ObjectOutputStream.PutField.put(String name, Object val) method.

public abstract void put(String name, Object val)

Parameters

  • name − The name of the serializable field.

  • val − The value to assign to the field.

Return Value

This method does not return a value.

Exception

  • IllegalArgumentException − If name does not match the name of a serializable field for the class whose fields are being written, or if the type of the named field is not Object.

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.

Example - Serialize a Message object with a nested custom Author object

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

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("message_obj2.ser"))) {
         Author author = new Author("Bob", "[email protected]");
         Message message = new Message("Hello!", author);
         oos.writeObject(message);
      }

      // Deserialize
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("message_obj2.ser"))) {
         Message msg = (Message) ois.readObject();
         System.out.println("Message: " + msg.text);
         System.out.println("Author: " + msg.author.name + ", " + msg.author.email);
      } catch (ClassNotFoundException e) {
         e.printStackTrace();
      }
   }

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

      String text;
      Author author;

      public Message(String text, Author author) {
         this.text = text;
         this.author = author;
      }

      private void writeObject(ObjectOutputStream oos) throws IOException {
         ObjectOutputStream.PutField fields = oos.putFields();
         fields.put("text", text);
         fields.put("author", author);  // writing custom object field
         oos.writeFields();
      }

      private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
         ObjectInputStream.GetField fields = ois.readFields();
         text = (String) fields.get("text", "");
         author = (Author) fields.get("author", null);
      }
   }

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

      String name;
      String email;

      public Author(String name, String email) {
         this.name = name;
         this.email = email;
      }
   }
}

Output

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

Message: Hello!
Author: Bob, [email protected]

Explanation

  • A nested custom object (Author) is serialized using put().

  • Demonstrates that put(String, Object) works for any serializable object, not just simple types.

Example - Serialize a Book object with a Date (Object type) using put(String, Object)

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

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;
import java.util.Date;

public class ObjectOutputStreamDemo {
    public static void main(String[] args) throws IOException {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("book.ser"))) {
            Book book = new Book("Effective Java", new Date());
            oos.writeObject(book);
        }

        // Read back
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("book.ser"))) {
            Book book = (Book) ois.readObject();
            System.out.println("Title: " + book.title);
            System.out.println("Published: " + book.publishedDate);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

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

        String title;
        Date publishedDate;  // Object field

        public Book(String title, Date publishedDate) {
            this.title = title;
            this.publishedDate = publishedDate;
        }

        private void writeObject(ObjectOutputStream oos) throws IOException {
            ObjectOutputStream.PutField fields = oos.putFields();
            fields.put("title", title);
            fields.put("publishedDate", publishedDate); // put Object (Date)
            oos.writeFields();
        }

        private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
            ObjectInputStream.GetField fields = ois.readFields();
            title = (String) fields.get("title", "Untitled");
            publishedDate = (Date) fields.get("publishedDate", null);
        }
    }
}

Output

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

Title: Effective Java
Published: Fri Apr 25 11:55:55 IST 2025

Explanation

  • put("publishedDate", publishedDate) stores a Date object (which implements Serializable).

  • Demonstrates that put(String, Object) works with standard Java library classes that implement Serializable.

  • On deserialization, the Date is restored properly using get("publishedDate", null).

java_io_objectoutputstream.putfield.htm
Advertisements