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



Description

The Java ObjectInputStream.getField get(String name, double val) method gets the value of the named double field from the persistent field, and if it wasn't serialized, it returns the default value you provide.

Declaration

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

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

The following example shows the usage of ObjectInputStream.getField get(String name, double 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 double var = 1.875d;
      
      // assign a new serialPersistentFields 
      private static final ObjectStreamField[] serialPersistentFields = {
         new ObjectStreamField("var", Double.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 = (double) fields.get("var", 0d);
      }

      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 −

1.875

Example - Read a Serialized double Field when Field is Present

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

      // Serialize object with a double field
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
         oos.writeObject(new Data(3.14));
         System.out.println("Object serialized with score = 3.14");
      }

      // Deserialize and read the field
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
         Data data = (Data) ois.readObject();
      }
   }

   static class Data implements Serializable {
      private static final long serialVersionUID = 1L;
      double score;

      public Data(double score) {
         this.score = score;
      }

      private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
         ObjectInputStream.GetField fields = ois.readFields();
         double score = fields.get("score", 0.0);  // Reads stored value
         System.out.println("Deserialized score: " + score);
      }
   }
}

Output

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

Object serialized with score = 3.14  
Deserialized score: 3.14

Explanation

  • Since the score field was serialized, it's correctly retrieved.

Example - Handling a Missing double Field

The following example shows the usage of ObjectInputStream.getField get(String name, double val) method. In this example, we don't write the score field, so during deserialization, the default value is returned.

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

      // Serialize object, but skip 'score' field manually
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
         oos.writeObject(new Data(5.5));  // The value is ignored in custom writeObject
      }

      // 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;
      transient double score;

      public Data(double score) {
         this.score = score;
      }

      private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
         ObjectInputStream.GetField fields = ois.readFields();
         double score = fields.get("score", 99.9);  // Default value will be used
         System.out.println("Deserialized score (defaulted): " + score);
      }
   }
}

Output

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

Exception in thread "main" java.lang.IllegalArgumentException: no such field score with type double

Explanation

  • Since the field was never written, get("score", 99.9) throws an IllegalArgumentException

java_io_objectinputstream.getfield.htm
Advertisements