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



Description

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

  • It sets the value of a double field when using custom serialization with ObjectOutputStream.

  • Used inside a class's writeObject(ObjectOutputStream oos) method.

  • You must call writeFields() after put() to actually write the data.

Declaration

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

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

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

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

         // get var
         var = 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 - Serialize a Product object with a price (double)

The following example shows the usage of ObjectOutputStream.PutField put(String name, double val) method. Write a double price field 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("product1.ser"))) {
         Product p = new Product("Laptop", 1499.99);
         oos.writeObject(p);

         ObjectInputStream ois = new ObjectInputStream( new FileInputStream("product1.ser"));
         Product p1 = (Product)ois.readObject();
         System.out.println("Product:: name = '" + p1.name + "' price = " + p1.price);
      }
   }

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

      String name;
      double price;

      public Product(String name, double price) {
         this.name = name;
         this.price = price;
      }

      private void writeObject(ObjectOutputStream oos) throws IOException {
         ObjectOutputStream.PutField fields = oos.putFields();
         fields.put("name", name);
         fields.put("price", price);  // putting a double value
         oos.writeFields();
      }

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

Output

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

Product:: name = 'Laptop' price = 1499.99

Explanation

  • The put() method is used to manually write the double value of price.

  • Useful when controlling which fields to serialize.

Example - Serialize a Sensor object and derive a double reading

The following example shows the usage of ObjectOutputStream.PutField put(String name, double val) method. We're storing a derived reading (e.g., Celsius converted from Fahrenheit) using put(String, double) 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.Serializable;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("sensor2.ser"))) {
         Sensor s = new Sensor("TempSensor", 98.6); // Fahrenheit
         oos.writeObject(s);
      }

      // Read it back
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("sensor2.ser"))) {
         Sensor s = (Sensor) ois.readObject();
         System.out.println("Deserialized: " + s.name + ", Celsius = " + s.celsiusReading);
      } catch (ClassNotFoundException e) {
         e.printStackTrace();
      }
   }

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

      String name;
      transient double fahrenheit; // not serialized
      double celsiusReading;

      public Sensor(String name, double fahrenheit) {
         this.name = name;
         this.fahrenheit = fahrenheit;
      }

      private void writeObject(ObjectOutputStream oos) throws IOException {
         ObjectOutputStream.PutField fields = oos.putFields();
         fields.put("name", name);
         fields.put("celsiusReading", (fahrenheit - 32) * 5 / 9); // derived double
         oos.writeFields();
      }

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

Output

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

Deserialized: TempSensor, Celsius = 37.0
java_io_objectoutputstream.putfield.htm
Advertisements