Java.io - SPLessons

Java.io ObjectOutputStream.PutField

Home > Lesson > Chapter 31
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io ObjectOutputStream.PutField

Java.io ObjectOutputStream.PutField

shape Introduction

The Java.io ObjectOutputStream.PutField is the class used to provide the programmatic access to persistent fields from input stream and written it to object output. The declaration will be as follows. Public class ObjectInputStream.PutField.PutField extends Object

Class Constructors

shape Table

Constructor Description
ObjectInputStream.PutField() The function of this constructor is to create the Java.io ObjectOutputStream.PutField instance.

Class Methods

shape Table

Method Description
abstract void put (String name, char val) The function of this method is to put value represented with character field in persistent field.
abstract void put (String name, boolean val) The function of this method is puts the value of the field represented from persistent field.
abstract void put (String name, byte val) The function of this method is to puts the value of the field from persistent field in byte.
abstract void put (String name, object val) The function of this method is to put value represented with object field in persistent field.

Inherited Methods

shape Description

From the following classes, methods are inherited to Java.io ObjectOutputStream.PutField class.
  • Java.io.Object

shape Examples

Usage of abstract void put (String name, char val) method. [c]import java.io.*; public class ObjectInputStream implements Serializable { public static void main(String[] args) { try { // creating a new file with an object output stream FileOutputStream out = new FileOutputStream("test.txt"); ObjectOutputStream oout = new ObjectOutputStream(out); // To write something in the file oout.writeObject(new Example()); oout.flush(); oout.close(); // creating an object input stream for the file created above ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt")); // To read an object from the stream and cast it to Example Example a = (Example) ois.readObject(); // To print var System.out.println("" + a.var); } catch (Exception ex) { ex.printStackTrace(); } } static public class Example implements Serializable { static boolean var = false; // To assign a new serial persistent fields private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField("var", Boolean.TYPE) }; private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { // To get the field and assign it at var ObjectInputStream.GetField fields = in.readFields(); // To get var var = fields.get("var", true); } private void writeObject(ObjectOutputStream out) throws IOException { // To write the variable string in ObjectStreamField array ObjectOutputStream.PutField fields = out.putFields(); fields.put("var", var); out.writeFields(); } } }[/c] Output The result will be as follows. [c]false[/c] Usage of abstract void put (String name, boolean val) method. [c]import java.io.*; public class ObjectOutputStream implements Serializable { public static void main(String[] args) { try { // creating a new file with an object output stream FileOutputStream out = new FileOutputStream("test.txt"); ObjectOutputStream oout = new ObjectOutputStream(out); // To write something in the file oout.writeObject(new Example()); oout.flush(); oout.close(); // creating an object input stream for the file created above ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt")); // To read an object from the stream and cast it to Example Example a = (Example) ois.readObject(); // To print var System.out.println"" + a.var); } catch (Exception ex) { ex.printStackTrace(); } } static public class Example implements Serializable { static byte var = 15; // To assign a new serial persistent fields private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField("var", Byte.TYPE) }; private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { // To get the field and assign it at string variable ObjectInputStream.GetField fields = in.readFields(); // To check if string is default, that means if it has no value byte b = 0; var = (byte) fields.get("var", b); } private void writeObject(ObjectOutputStream out) throws IOException { // To write into the object stream field array the variable var ObjectOutputStream.PutField fields = out.putFields(); fields.put("var", var); out.writeFields(); } } }[/c] The result will be as follows. [java]Output: 15[/java]

Summary

shape Key Points

  • The Java.io ObjectOutputStream.PutField is the class used to provide the programmatic access to persistent fields from input stream and written it to object output.