Java.io - SPLessons

Java.io ObjectOutputStream

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

Java.io ObjectOutputStream

Java.io ObjectOutputStream

shape Introduction

The Java.io ObjectOutputStream is one of the significant class in java.io packages. The main feature of this Java.io ObjectOutputStream is to write the objects instead of writing bytes, because of this the performance is little better than other writers. The object output stream writes only the objects which are serialized in java. The class should implements the serializable while writing object from that class. The object output stream class is subclass to output stream class and wrap output stream into the object output stream for to write objects.

shape Conceptual figure

Class Declaration

shape Declaration

Java.io ObjectOutputStream class is declared below: Public class ObjectOutputStream extends OutputStream implements ObjectOutput, ObjectStreamConstants

Class Constructors

shape Table

Constructor Description
Protected ObjectOutputStream() The function of this constructor is to create the object output stream instance for reimplemetation.
ObjectOutputStream(OutputStream out) The function of this constructor is to create the object output stream instance and used to write to output stream.

Class Methods

shape Table

Method Description
void writeFileds() The function of this method is to write the fields to the object output stream.
void use ProtocalVersion(int version) The function of this method is to represent the protocol version of the stream while writing a stream.
protected void drian() The function of this method is to drain the buffered data related to object output stream.
protected void annotateClass(Class<?>cl) Because of this method accessing data related to class for to store in the stream which is implemented by subclasses.
protected void writeStreamHeader() The function of this method is to append or to prepends the headers to stream.
void defaultWriteObject() The function of this method is to write the dynamic and non - transient of the present class to the stream.
Protected object replaceObject(Object obj) The function of this method is to allow the subclasses to replace objects during serialization in object output stream.

Inherited Methods

shape Description

From the following classes, methods are inherited to object output stream class.
  • Java.io.Object
  • Java.io.OutputStream
  • Java.io.ObjectOutput

shape Examples

Usage of protected void drian() method. [c]import java.io.*; //public class ObjectOutputStreamDemo { public class ObjectOutputStreamDemo extends ObjectOutputStream { public ObjectOutputStreamDemo(OutputStream out) throws IOException { super(out); } public static void main(String[] args) { int i = 111111; try { // creating a new file with an ObjectOutputStream FileOutputStream out = new FileOutputStream("test.txt"); ObjectOutputStreamDemo oout = new ObjectOutputStreamDemo(out); // To write something in the file oout.writeInt(i); oout.writeInt(2222222); // To drain the stream oout.drain(); // To close the stream oout.close(); // creating an ObjectInputStream for the file we created before ObjectInputStream ois = new ObjectInputStream(new FileInputStream( "test.txt")); // To read and print an int value System.out.println("" + ois.readInt()); // To read and print an int value System.out.println("" + ois.readInt()); } catch (Exception ex) { ex.printStackTrace(); } } } [/c] Output The result will be as follows. [c]111111 2222222 [/c] Usage of void writeFileds() method. [c]import java.io.*; public class ObjectOutputStream { public static void main(String[] args) { double d = 1.59875d; try { // creating a new file with an ObjectOutputStream 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 ObjectInputStream for the file we created before 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(); // print variable System.out.println("" + a.var); } catch (Exception ex) { ex.printStackTrace(); } } static public class Example implements Serializable { static int var = 11111; // assign a new serialPersistentFields private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField("var", Integer.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", 0); } private void writeObject(ObjectOutputStream out) throws IOException { // write into the ObjectStreamField array the variable string ObjectOutputStream.PutField fields = out.putFields(); fields.put("var", var); out.writeFields(); } } }[/c] Output The result will be as follows. [c]11111[/c]

Summary

shape Key Points

  • The Java.io ObjectOutputStream is one of the significant class in java.io packages.
  • The main feature of this object output stream is to write the objects instead of writing bytes, because of this the performance is little better than other writers.
  • The object output stream writes only the objects which are serialized in java.
  • The class should implements the serializable while writing object from that class.