Java.io - SPLessons

Java.io ObjectInputStream

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

Java.io ObjectInputStream

Java.io ObjectInputStream

shape Introduction

The Java.io ObjectInputStream is one of the significant class in java.io packages. The main feature of this object input stream is to read the objects instead of reading bytes, because of this the performance is little better than other readers. The Java.io ObjectInputStream reads only the objects which are serialized in java. The class should implements the serializable while reading object from that class. The object input stream class is subclass to input stream class and wrap input stream into the object input stream for to read objects.

shape Conceptual figure

Class Declaration

shape Declaration

Java.io ObjectInputStream class is declared below: Public  class ObjectInputStream extends InputStream  implements ObjectInput, ObjectStreamConstants

Class Constructors

shape Table

Constructor Description
Protected ObjectInputStream() The function of this constructor is to create the object input stream instance for reimplemetation.
ObjectInputStream(InputStream in) The function of this constructor is to create the object input stream instance and used to read from input stream.

Class Methods

shape Table

Method Description
int read  () By using this method it reads the bytes of data from the input stream.
void read fully( byte[] buf) The function of this method is to read the bytes from the input stream and store in array buffer.
int skipBytes(int n) From the input stream this method skips over and throw out ‘n’ bytes of data.
int read(byte[] buf, int off, int n) By using this method it reads the data up to 'n' bytes from the input stream to an array  of the bytes.
String readUTF() The function of this method is to read the content in the string format and have encoded by UTF-8 format.
Object readObject() The function of this method is to read the object from input stream

Inherited Methods

shape Description

From the following classes, methods are inherited to object input stream class.
  • Java.io.Object
  • Java.io.InputStream
  • Java.io.ObjectInput

shape Examples

Usage of Object readObject() method. [c]import java.io.*; public class ObjectInputStream { public static void main(String[] args) { String s = "Hello World"; byte[] b = {'S', 'P', 'L', 'E', 'S', 'S', 'O', 'N', 'S'}; 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(s); oout.writeObject(b); oout.flush(); // creating an ObjectInputStream for the file we created before ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt")); // Read and print an object and to cast it as string System.out.println("" + (String) ois.readObject()); // Read and print an object and cast it as string byte[] read = (byte[]) ois.readObject(); String s2 = new String(read); System.out.println("" + s2); } catch (Exception ex) { ex.printStackTrace(); } } }[/c] Output The result will be as follows. [c]Hello World SPLESSONS[/c] Usage of String readUTF() method. [c]import java.io.*; public class ObjectInputStream { public static void main(String[] args) { String s = "Hello World!"; try { // creating a new file with an ObjectOutputStream FileOutputStream out = new FileOutputStream("test.txt"); ObjectOutputStream oout = new ObjectOutputStream(out); // write something in the file oout.writeUTF(s); oout.writeUTF("SP LESSONS"); oout.flush(); // creating an ObjectInputStream for the file we created before ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt")); // for to read and print the string System.out.println("" + ois.readUTF()); // for to read and print the string System.out.println("" + ois.readUTF()); } catch (Exception ex) { ex.printStackTrace(); } } }[/c] Output The result will be as follows. [c]Hello World! SP LESSONS[/c]

Summary

shape Key Points

  • The Java.io ObjectInputStream is one of the significant class in java.io packages.
  • The main feature of this object input stream is to read the objects instead of reading bytes, because of this the performance is little better than other readers.
  • The object input stream reads only the objects which are serialized in java.
  • The class should implements the serializable while reading object from that class.