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



Description

The Java ObjectInputStream.getField get(String name, float val) method gets the value of the named float 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, float val) method.

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

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

      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 float Field when Field is Present

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

      // Serialize object with a float field
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) {
         Data data = new Data(3.14f);
         oos.writeObject(data);
         System.out.println("Object written.");
      }

      // Deserialize and read using GetField
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
         Data data = (Data) ois.readObject();
      }
   }

   static class Data implements Serializable {
      private static final long serialVersionUID = 1L;
      float temperature;

      Data(float temperature) {
         this.temperature = temperature;
      }

      private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
         ObjectInputStream.GetField fields = ois.readFields();
         float temp = fields.get("temperature", 0.0f);
         System.out.println("Deserialized temperature: " + temp);
      }
   }
}

Output

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

Object written.
Deserialized temperature: 3.14

Explanation

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

Example - Handling a Missing float Field

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

ObjectInputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
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) {
      // Serialization
      try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
         ObjectOutputStream oos = new ObjectOutputStream(bos)) {

         // Create and serialize object
         TemperatureReading original = new TemperatureReading(23.5f);
         oos.writeObject(original);
         System.out.println("Original: " + original);

         byte[] serializedData = bos.toByteArray();

         // Deserialization
         try (ByteArrayInputStream bis = new ByteArrayInputStream(serializedData);
            ObjectInputStream ois = new ObjectInputStream(bis)) {

            TemperatureReading deserialized = (TemperatureReading) ois.readObject();
            System.out.println("Deserialized: " + deserialized);
         }
      } catch (IOException | ClassNotFoundException e) {
         e.printStackTrace();
      }
   }
   static class TemperatureReading implements Serializable {
      private float temp;

      public TemperatureReading(float temp) {
         this.temp = temp;
      }

      // Custom serialization (NEW - skips writing 'temp')
      private void writeObject(ObjectOutputStream out) throws IOException {
         ObjectOutputStream.PutField fields = out.putFields();
         // Intentionally don't write 'temp' field
         out.writeFields();
      }

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

         ObjectInputStream.GetField fields = in.readFields();
         this.temp = fields.get("temp", -273.15f);
      }

      @Override
      public String toString() {
         return String.format("Temperature: %.1f C", temp);
      }
   }
}

Output

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

Original: Temperature: 23.5 
Deserialized: Temperature: 0.0 

Explanation

  • Added a writeObject method that skips writing the temp field.

  • The readObject method now encounters a serialized object without the temp field.

java_io_objectinputstream.getfield.htm
Advertisements