Java - ObjectInputStream.GetField get(String name, boolean val) method



Description

The Java ObjectInputStream.getField get(String name, boolean val) method gets the value of the named boolean field from the persistent field.

Declaration

Following is the declaration for java.io.ObjectInputStream.getField.get(String name, boolean val) method.

public abstract boolean get(String name, boolean val)

Parameters

  • name − The name of the field.

  • val − The default value to use if name does not have a value.

Return Value

This method returns the value of the named boolean field.

Exception

  • IOException − If there are I/O errors while reading from the underlying InputStream.

  • IllegalArgumentException − If type of name is not serializable or if the field type is incorrect.

Example - Usage of ObjectInputStream.getField get(String name, boolean val) method

The following example shows the usage of ObjectInputStream.getField get(String name, boolean val) method.

ObjectInputStreamDemo.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 ObjectInputStreamDemo 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 string
         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 - Read a boolean field normally

The following example shows the usage of ObjectInputStream.getField get(String name, boolean val) method. This example shows how to read a boolean field that was serialized.

ObjectInputStreamDemo.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 ObjectInputStreamDemo {
   public static void main(String[] args) throws Exception {
      String filename = "bool1.ser";

      // Serialize
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
         Flag flag = new Flag(true);
         oos.writeObject(flag);
      }

      // Deserialize
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
         Flag f = (Flag) ois.readObject();
      }
   }

   static class Flag implements Serializable {
      private static final long serialVersionUID = 1L;
      boolean isEnabled;

      Flag(boolean isEnabled) {
         this.isEnabled = isEnabled;
      }

      private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
         ObjectInputStream.GetField fields = ois.readFields();
         boolean flag = fields.get("isEnabled", false);
         System.out.println("Deserialized isEnabled: " + flag);
      }
   }
}

Output

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

Deserialized isEnabled: true

Explanation

  • The get("isEnabled", false) call retrieves the serialized value true.

  • The second argument (false) is a default fallback, only used if the field is missing.

Example - Field was not serialized − get() returns the default

The following example shows the usage of ObjectInputStream.getField get(String name, boolean val) method. Here, we skip serializing the isEnabled field, so get() returns the default (false).

ObjectInputStreamDemo.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 ObjectInputStreamDemo {
   public static void main(String[] args) throws Exception {
      String filename = "bool2.ser";

      // Serialize without writing isEnabled field
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
         oos.writeObject(new Flag(true)); // isEnabled = true, but we skip writing it
      }

      // Deserialize
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
         Flag f = (Flag) ois.readObject();
      }
   }

   static class Flag implements Serializable {
      private static final long serialVersionUID = 1L;
      boolean isEnabled;

      Flag(boolean isEnabled) {
         this.isEnabled = isEnabled;
      }

      private void writeObject(ObjectOutputStream oos) throws IOException {
         ObjectOutputStream.PutField fields = oos.putFields();
         // Do NOT write the isEnabled field
         oos.writeFields();
      }

      private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
         ObjectInputStream.GetField fields = ois.readFields();
         boolean flag = fields.get("isEnabled", false); // Should fallback to false
         System.out.println("Deserialized isEnabled (defaulted): " + flag);
      }
   }
}

Output

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

Deserialized isEnabled (defaulted): false

Explanation

  • isEnabled wasn't written, so fields.get("isEnabled", false) uses the fallback value (false).

java_io_objectinputstream.getfield.htm
Advertisements