Java.io - SPLessons

Java.io ObjectInputStream.GetField

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

Java.io ObjectInputStream.GetField

Java.io ObjectInputStream.GetField

shape Introduction

The Java.io ObjectInputStream.GetField is the class used to access to read persistent fields from input stream. Class declaration will be as follows. Java.io ObjectInputStream.GetField class is declared below: Public class ObjectInputStream.GetField extends Object  

Class Constructors

shape Table

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

Class Methods

shape Table

Method Description
abstract boolean defaulted(String name) The function of this method is to check the current field is default or not to return boolean value.
abstract boolean get (String name, boolean val) The function of this method is return the value of the field represented from persistent field.
abstract byte get (String name, byte val) The function of this method is to return the value of the field from persistent field in byte.
abstract ObjectStreamClass getObjectStreamClass() The function of this method is to obtain the object stream class to access the field in stream.

Inherited Methods

shape Description

From the following classes, methods are inherited to ObjectInputStream.GetField class.
  • Java.io.Object

shape Examples

Usage of abstract boolean defaulted(String name) 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 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt")); // To read an object from the stream and cast it to the Example Example a = (Example) ois.readObject(); // To get if variable string is default in Example class System.out.println("" + a.isDefault); // To print the string System.out.println("" + a.string); } catch (Exception ex) { ex.printStackTrace(); } } static public class Example implements Serializable { static String string = "SP LESSONS"; static boolean isDefault; // To assign a new serial persistent fields private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField("string", String.class) }; // creating a custom read object method 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 isDefault = fields.defaulted("string"); string = (String) fields.get("string", null); } // creating a custom write object method private void writeObject(ObjectOutputStream out) throws IOException { // To write the variable string in object stream field array ObjectOutputStream.PutField fields = out.putFields(); fields.put("string", string); out.writeFields(); } } }[/c] Output The result will be as follows. [c]false SP LESSONS [/c] Usage of abstract boolean get (String name, boolean 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]

Summary

shape Key Points

  • The Java.io ObjectInputStream.GetField is the class used to access to read persistent fields from input stream.