Java I/O Object Streams

In this section we will discuss the Java IO Object Streams.

To work with the I/O of objects Java provided the support of Object stream.
Object stream classes implements either ObjectInput or the ObjectOutput
interfaces. ObjectInput is the subinterface of DataInput and the ObjectOutput is
the subinterface of DataOutput interface.

ObjectInput : ObjectInput is an interface which is a
subinterface of DataInput interface that facilitate for reading the objects.

Class that implements the ObjectInput interface.

Class

Description

ObjectInputStream

ObjectInputStream retrieves the previously serialized objects.
This class can read only those objects which are of type java.io.Serializable or
java.io.Externalizable.

Methods of ObjectInput

available() : This method is used to find out the
readable number of bytes.

int available() throws IOException

close() : This method is used to close the input stream.

void close() throws IOException

read() : This method is used to read the data's byte.

int read() throws IOException

read(byte[] b) : This method is used to read into an
array of bytes.

int read(byte[] b) throws IOException

read(byte[] b, int off, int len) : This method is used to
read into an array of bytes.

int read(byte[] b, int off, int len)
throws IOException

readObject() : This method is used to read the object
that support java.io.Serializable interface and also returns the object.

Object readObject() throws ClassNotFoundException,
IOException

skip(long n) : This method is used to skipped over the n
bytes.

long skip(long n) throws IOException

ObjectOutput : ObjectOutput is an interface that extends the
DataOutput interface which facilitate to write objects.

Class that implements the ObjectOutput interface.

Class

Description

ObjectOutputStream

This class is used to write the primitive data types and graphs of Java
objects into an output stream. This class can write only those objects which
support the java.io.Serializable interface, into the output
stream. Primitive data types and strings can be written to the output stream
using the method of DataOutput interface

Methods of ObjectOutput

close() : This method is used to close the output stream.

void close() throws IOException

flush() : This method is used to flushes the output
stream.

void flush() throws IOException

write(byte[] b) : This method is used to write an array of
byte into the output stream.

void write(byte[] b) throws IOException

write(byte[] b, int off, int len) : This method is used
to write an array of byte of the specified length 'len' and started from the
value of 'off', into an output stream.

void write(byte[] b, int off, int len)
throws IOException

write(int b) : This method is used to write for the
integer argument to the output stream.

void write(int b) throws IOException

writeObject(Object obj) : This method is used to write
object into the output stream.