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



Description

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

  • It's used inside a class’s custom writeObject(ObjectOutputStream oos) method.

  • It assigns a value to a float field by name.

  • After setting values using put(), you must call writeFields() to actually write them into the stream.

Declaration

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

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

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

The following example shows the usage of ObjectOutputStream.PutField put(String name, float 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 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 var
         ObjectInputStream.GetField fields = in.readFields();

         // get var
         var = 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 - Serialize a Measurement object with a float value

The following example shows the usage of ObjectOutputStream.PutField put(String name, float val) method. We're writing a float field (value) manually using put().

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("measurement1.ser"))) {
         Measurement m = new Measurement("Weight", 72.5f);
         oos.writeObject(m);

         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("measurement1.ser"));
         Measurement m1 = (Measurement)ois.readObject();
         System.out.println("Measurement:: type = '" +m1.type + "' value = " + m1.value);
      }
   }

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

      String type;
      float value;

      public Measurement(String type, float value) {
         this.type = type;
         this.value = value;
      }

      private void writeObject(ObjectOutputStream oos) throws IOException {
         ObjectOutputStream.PutField fields = oos.putFields();
         fields.put("type", type);
         fields.put("value", value); // write float value
         oos.writeFields();
      }

      private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
         ObjectInputStream.GetField fields = ois.readFields();
         type = (String) fields.get("type", "Unknown");
         value = fields.get("value", 0.0f);
      }
   }
}

Output

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

Measurement:: type = 'Weight' value = 72.5

Explanation

  • The value field (a float) is written explicitly using put.

  • On reading, it uses get("value", default) to restore it.

Example - Write a derived float field (e.g., discount percentage)

The following example shows the usage of ObjectOutputStream.PutField put(String name, float val) method. We're serializing a Product and manually compute and store a discountRate as a float.

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 {
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("product_discount.ser"))) {
         Product p = new Product("Laptop", 1000f, 800f); // price: 1000 → sale: 800
         oos.writeObject(p);
      }

      // Read it back
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("product_discount.ser"))) {
         Product p = (Product) ois.readObject();
         double discount = Math.round(p.discountRate * 100) / 100.0;
         System.out.println("Product: " + p.name + ", Discount Rate: " + discount + "%");
      } catch (ClassNotFoundException e) {
         e.printStackTrace();
      }
   }

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

      String name;
      transient float originalPrice;
      transient float salePrice;
      float discountRate;

      public Product(String name, float originalPrice, float salePrice) {
         this.name = name;
         this.originalPrice = originalPrice;
         this.salePrice = salePrice;
      }

      private void writeObject(ObjectOutputStream oos) throws IOException {
         ObjectOutputStream.PutField fields = oos.putFields();
         fields.put("name", name);
         float discount = (1 - (salePrice / originalPrice)) * 100;
         fields.put("discountRate", discount);  // derived float value
         oos.writeFields();
      }

      private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
         ObjectInputStream.GetField fields = ois.readFields();
         name = (String) fields.get("name", "Unknown");
         discountRate = fields.get("discountRate", 0.0f);
      }
   }
}

Output

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

Product: Laptop, Discount Rate: 20.0%
java_io_objectoutputstream.putfield.htm
Advertisements