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



Description

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

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

  • Allows you to assign a char value to a named field for serialization.

  • After calling put(), you must call oos.writeFields() to actually write the fields to the stream.

Declaration

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

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

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

The following example shows the usage of ObjectOutputStream.PutField put(String name, char 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 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 - Serialize a Student object with a grade (char)

The following example shows the usage of ObjectOutputStream.PutField put(String name, char val) method. We're writing a student's grade using put() 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, ClassNotFoundException {
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student1.ser"))) {
         Student s = new Student("Alice", 'A');
         oos.writeObject(s);

         ObjectInputStream ois = new ObjectInputStream( new FileInputStream("student1.ser"));
         Student s1 = (Student)ois.readObject();
         System.out.println("Student::  name = '" + s1.name + "' , grade = " + s1.grade);
      }
   }

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

      String name;
      char grade;

      public Student(String name, char grade) {
         this.name = name;
         this.grade = grade;
      }

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

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

Output

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

Student::  name = 'Alice' , grade = A

Explanation

  • The grade field is written manually as a char using put().

Example - Serialize a MenuItem with a keyboard shortcut (char)

The following example shows the usage of ObjectOutputStream.PutField put(String name, char val) method. We're serializing a menu item, and explicitly store its shortcut key (like 'S' for Save, 'O' for Open).

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("menuitem.ser"))) {
         MenuItem item = new MenuItem("Save", 'S');
         oos.writeObject(item);
      }

      // Read back and print
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("menuitem.ser"))) {
         MenuItem readItem = (MenuItem) ois.readObject();
         System.out.println("Deserialized: " + readItem.label + " [" + readItem.shortcut + "]");
      } catch (ClassNotFoundException e) {
         e.printStackTrace();
      }
   }

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

      String label;
      char shortcut; // keyboard shortcut, e.g., 'S' for Save

      public MenuItem(String label, char shortcut) {
         this.label = label;
         this.shortcut = shortcut;
      }

      private void writeObject(ObjectOutputStream oos) throws IOException {
         ObjectOutputStream.PutField fields = oos.putFields();
         fields.put("label", label);
         fields.put("shortcut", shortcut);  // manually writing char
         oos.writeFields();
      }

      private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
         ObjectInputStream.GetField fields = ois.readFields();
         label = (String) fields.get("label", "Untitled");
         shortcut = fields.get("shortcut", '?'); // fallback
      }
   }
}

Output

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

Deserialized: Save [S]

Explanation

  • The shortcut field is a char representing the menu's hotkey (like 'S' for "Save").

  • It's stored manually using put(String, char).

  • This is useful in UIs or config files where shortcut keys are part of the metadata.

java_io_objectoutputstream.putfield.htm
Advertisements