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



Description

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

Declaration

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

public abstract boolean get(String name, char 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 char 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, char val) method

The following example shows the usage of ObjectInputStream.getField get(String name, char 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 variable of a
         System.out.println("" + a.var);

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

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

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

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

         // get var
         var = (char) fields.get("var", 'a');
      }

      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 −

a

Example - Read a Serialized char Field Normally

The following example shows the usage of ObjectInputStream.getField get(String name, char val) method. This example demonstrates reading a char field that was properly 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 = "char1.ser";

      // Serialize
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
         Data data = new Data('A');
         oos.writeObject(data);
      }

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

   static class Data implements Serializable {
      private static final long serialVersionUID = 1L;
      char letter;

      Data(char letter) {
         this.letter = letter;
      }

      private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
         ObjectInputStream.GetField fields = ois.readFields();
         char letter = fields.get("letter", 'X'); // Read serialized field
         System.out.println("Deserialized letter: " + letter);
      }
   }
}

Output

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

Deserialized letter: A

Explanation

  • The get("letter", 'X') retrieves the stored value 42.

  • The second argument ((byte) 0) is only used if the field is missing.

Example - Handling a Missing char Field

The following example shows the usage of ObjectInputStream.getField get(String name, char val) method. This example intentionally skips writing the char field during serialization, so get() returns the default 'Z'.

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 = "char2.ser";

      // Serialize without writing 'letter' field
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
         oos.writeObject(new Data('A')); // The letter field exists but isn't written
      }

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

   static class Data implements Serializable {
      private static final long serialVersionUID = 1L;
      char letter;

      Data(char letter) {
         this.letter = letter;
      }

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

      private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
         System.out.println(" ---- inside readObject ---");
         ObjectInputStream.GetField fields = ois.readFields();

         char letter;

         if (! fields.defaulted("letter")) {
            System.out.println("Field 'letter' was defaulted (not found in stream).");
            letter = 'Z'; // Explicitly assign a default value
         } else {
            letter = fields.get("letter", 'Z'); // Read serialized field (fallback)
         }

         System.out.println("Deserialized letter: " + letter);
      }
   }
}

Output

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

---- inside readObject ---
Field 'letter' was defaulted (not found in stream).
Deserialized letter: Z

Explanation

  • Since letter wasn't written, fields.get("letter", 'Z') falls back to Z.

  • This handles removed fields or missing data gracefully.

java_io_objectinputstream.getfield.htm
Advertisements