OutputStream in Java

OutputStream class in Java is an abstract class that is meant for writing the binary data from Java application to a file/console/network. It is a superclass of all output stream classes used to write binary data and contains methods for writing binary data into the file/console/network. It implements a Closeable and Flushable interface.

public abstract class OutputStream
extends Object
implements Closeable, Flushable

When we write data into the stream then data is not saved in the file directly, it is stored only in the output stream memory, and to save those data from that memory to the file, the flush() method must be called. Whenever we call the write() method then first saved inside the output stream memory and to save those data from the output stream memory to file we must call flush() method.

Up to Java 1.4 version flush() method was directly available in the OutputStream class, but in Java 1.5 version Flushable interface was introduced which contains flush() method. And OutputStream class implements the Flushable interface.

package java.io;
import java.io.IOException;
public interface Flushable {
    void flush() throws IOException;
}
package java.io;
public abstract class OutputStream
implements Closeable, Flushable {
      ………
}

Java OutputStream class methods

There are methods available in OutputStream class are,

Modifier and typeMethodDescription
abstract voidwrite(int b)Write specified byte
voidwrite(byte[] b)Write b.length bytes from the specified byte array
voidwrite(byte[] b, int offset, int len)It writes len bytes from the specified byte array starting at offset off.
voidflush()Flushes this output stream and forces any buffered output bytes to be written out.
voidclose()It closes the stream and releases any system resources associated with it.
OutputStreamnullOutputStream()Returns a new OutputStream which discards all bytes. (Since Java11)

The write() method directly can’t save the data to the file. It can store data only to the stream so to move data from the stream into the file we must call the flush() method.

Sub classes of Java OutputStream

Java OutputStream class in Java has different sub classes based on the sources and formats to the data. These sub classes are,

Among these 8 classes FileOutputStream, DataOutputStream, PrintStream and ObjectOutputStream are important classes,

FileOutputStreamIt writes one-byte data to the file. It is the basic destination for the file.
DataOutputStreamWrite data in primitive (int, long, ..) size format. It is used to add the capability to another output stream to write data as primitive types.
BufferedOutputStreamWrite data in buffers. It is used to improve the writing performance of other streams.
PrintStreamIt writes data as it is. It is the most useful output stream class to write data.
ObjectOutputStreamWrite data in Object format. It is used for deserialization.

None of these subclasses having a 0 parameter constructor because it is a stream-based connection where one end is a Java application. Therefore second end information must be passed as input to the constructor.

Among all 8 OutputStream subclasses, only FileOutputStream can directly connect to the file. The remaining all other 7 classes need the help from FileOutputStream class to connect to the file. They connect to FileOutputStream and FileOutputStream connect to the file. So, we can say the basic source of connecting to file and writing data is FileOutputStream class and the remaining classes are depending on it.

Why SequenceInputStream is there but SequenceOutputStream is not there? SequenceInputStream reads input from multiple streams sequentially one after one with single stream. But there is no need of writing the same data in multiple stream that’s why SequenceOutputStream don’t given. Similarly, StringBufferInputStream is there but StringBufferOutputStream is not there.

DataOutputStream and ObjectOutputStream

The DataOutputStream and ObjectOutputStream class are implemented from DataOutput and ObjectOutput interface respectively. And the ObjectOutput interface is a sub-interface of the DataOutput interface.

ObjectOutput => ObjectOutputStream
DataOutput => DataOutputStream

// DataOutput.java
package java.io;
interface DataOutput {
……
}

// ObjectInput.java
package java.io;
public interface ObjectOutput extends
DataOutput, AutoCloseable {
……
}

InputStream vs OutputStream

InputStreamOutputStream
It is a subclass of a Closeable interface.It is a subclass of a Closeable and Flushable interface.
It is used for reading binary data from the file.It is used for writing binary data into the file.
InputStream class is a superclass of all input stream type classes for reading binary data from the file.OutputStream class is a superclass of all output stream type classes for writing binary data into the file.
It contains total 9 sub classes. Important sub classes are FileInputStream, DataInputStream and ObjectInputStream.It contains total 8 sub classes. Important sub classes are FileOutputStream, DataOutputStream, PrintStream and ObjectOutputStream.
FileInputStream is the basic source for connecting to the file to read data. The remaining all subclasses are dependent on FileInputStream. Without FileInputStream we can’t read data from the file.FileOutputStream is the basic source for connecting to the file to write data. The remaining all subclasses are dependent on FileOutputStream. Without FileOutputStream we can’t write data to the file.

Similarity,

  • Both are subclasses of java.lang.Object class and implements a Closeable interface.
  • Both belong to the java.io package.
  • They are abstract classes.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *