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



Description

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

  • It's used inside a class's private void writeObject(ObjectOutputStream oos) method.

  • You use oos.putFields() to get a PutField object.

  • Then, you use put(name, value) to assign values to specific fields.

  • Finally, you call oos.writeFields() to write them to the stream.

Declaration

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

public abstract void put(String name, boolean 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 boolean.

Example - Usage of ObjectOutputStream.PutField put(String name, boolean val) method

The following example shows the usage of ObjectOutputStream.PutField put(String name, boolean val) 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.ObjectStreamField;
import java.io.Serializable;

public class ObjectOutputStreamDemo implements Serializable {
   public static void main(String[] args) {
      try {
         // create a new file with an ObjectOutputStream
         FileOutputStream out = new FileOutputStream("test.txt");
         ObjectOutputStream oout = new ObjectOutputStream(out);

         // write something in the file
         oout.writeObject(new Example());
         oout.flush();
         oout.close();

         // create an ObjectInputStream for the file we created before
         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

         // read an object from the stream and cast it to Example
         Example a = (Example) ois.readObject();

         // print var of a
         System.out.println("" + a.var);

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

   static public class Example implements Serializable {
      static boolean var = false;
      
      // assign a new serialPersistentFields 
      private static final ObjectStreamField[] serialPersistentFields = {
         new ObjectStreamField("var", Boolean.TYPE)
      };

      private void readObject(ObjectInputStream in)
         throws IOException, ClassNotFoundException {

         // get the field and assign it at var
         ObjectInputStream.GetField fields = in.readFields();

         // get var
         var = fields.get("var", true);
      }

      private void writeObject(ObjectOutputStream out) throws IOException {

         // write into the ObjectStreamField array the variable var
         ObjectOutputStream.PutField fields = out.putFields();
         fields.put("var", var);
         out.writeFields();
      }
   }
}

Output

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

false

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 - Conditionally exclude a boolean value from serialization

The following example shows the usage of ObjectOutputStream.PutField put(String name, boolean val) method. We only serialize isAdmin = true; skip it if false.

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_bool2.ser"))) {
         Admin admin = new Admin("Bob", false); // This will skip 'isAdmin'
         oos.writeObject(admin);

         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("user_bool2.ser"));
         Admin ad = (Admin)ois.readObject();
         System.out.println(" Admin:: username= " + ad.username + " isAdmin= " + ad.isAdmin);
      }
   }

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

      String username;
      boolean isAdmin;

      public Admin(String username, boolean isAdmin) {
         this.username = username;
         this.isAdmin = isAdmin;
      }

      private void writeObject(ObjectOutputStream oos) throws IOException {
         ObjectOutputStream.PutField fields = oos.putFields();
         fields.put("username", username);
         if (isAdmin) {
            fields.put("isAdmin", true); // Only write if true
         }
         oos.writeFields();
      }

      private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
         ObjectInputStream.GetField fields = ois.readFields();
         username = (String) fields.get("username", "guest");
         isAdmin = fields.get("isAdmin", false); // will default to false if not present
      }
   }
}

Output

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

Admin:: username= Bob isAdmin= false

Explanation

  • The boolean isAdmin is only written if true.

  • If the field is missing during deserialization, it defaults to false via fields.get("isAdmin", false).

java_io_objectoutputstream.putfield.htm
Advertisements