IO Streams in Java –  InputStream and OutputStream

IO Streams in Java

A stream is a logical connection between the java program and file. In Java, a stream is a sequence of data that is composed of bytes. Since its structure looks like a stream of water that flows continually, it is called a stream. In this post, we are going to discuss very important topic IO streams in Java.

IO Streams is the part of the program that processes the input and produces the output. The concept of the stream is used to make the IO operation fast. All the classes necessary for input and output operations are included in the java.io package. Through the Java I/O API, file handling can be performed in the Java.

1. InputStream

The InputStream class is also a type of an abstract class. It is a superclass among other classes that represent the input stream of bytes.

Methods used by the InputStream

The following are the methods of the InputStream:

  • public abstract int read():

This method is used to read the next byte of data from the input stream and at the end of the file, it returns -1.

  • public int available():

This method is used to figure out the number of bytes that can be read from the present input stream.

  • public void close():

This method closes the current input stream.

Example: 

InputStream Hierarchy:

  1. FileInputStream
  2. ByteArrayInputStream
  3. FilterInputStream
  4. DataInputStream
  5. BufferedInputStream
  6. PushBackInputStream
  7. PipedInputStream
  8. ObjectInputStream

1. FileInputStream

The Java FileInputStream class is used to get the input bytes from a file. It reads byte-oriented data like image data, video, and audio which are streams of raw bytes. This class can also read character-stream data but the FileReader class is more preferred for such data than the FileInputStream class.

2. ByteArrayInputStream

There are two parts in the ByteArrayInputStream and they are ByteArray and InputStream. This class reads byte array as an input stream. It makes use of an internal buffer to read data from a byte array. The buffer of the ByteArrayInputStream automatically increases on the basis of the data.

3. FilterInputStream

The FilterInputStream Class is not used individually in Java as it provides various subclasses to add more functionality. BufferedInputStream and DataInputStream are some of the subclasses of this class that are used to implement the InputStream.

3.1 DataInputStream

The function of the DataInputStream Class in Java is to permit an application to read primitive Java data types from the input stream in a way that is not dependent to the machine. This class normally makes use of the data output stream in order to write data which can be read by a data input stream in the future.

3.2 BufferedInputStream

The Java BufferedInputStream Class reads the information from the stream. It makes use of the buffer mechanism internally in order to increase the performance of the program.

These are the main points to remember about BufferedInputStream:

  • The internal buffer is automatically refilled many bytes at a time from the contained input stream when the bytes from the stream are skipped or read.
  • An internal buffer array comes into existence when a BufferedInputStream comes into existence.

3.3 PushbackInputStream

In Java, the PushbackInputStream class is used to override the InputStream. This class increases the functionality of another input stream and is used to unread a byte that has already been read and is also used to push back one byte.

4. PipedInputStream

PipedInputStream class should be connected to a PipedOutPutStream. So, both streams are connected with each other using the connect method of PipedOutStream class.

5. ObjectInputStream

In Java, the main function of the ObjectInputStream class is to deserialize objects and primitive data written through the ObjectOutputStream.

 

2. OutputStream

The OutputStream class is a type of abstract class. It is a superclass among other classes that represents the output stream of bytes. Here, the output stream accepts output bytes and moves them to some sink.

Methods used by the OutputStream

The following are the methods of the OutputStream:

  • public void write(int):

This method writes a byte to the present output stream.

  • public void write(byte[]):

This method writes an array of byte to the present output stream.

  • public void flush():

This method is used to flush the present output stream.

  • public void close():

This method closes the present output stream.

Example: 

OutputStream Hierarchy

1. FileOutputStream

2. ByteArrayOutputStream

3. FilterOutputStream

3.1 DataOutputStream

3.2 BufferedOutputStream

3.3 PrintStream

4. PipedOutputStream

5. ObjectOutputStream

1. FileOutputStream

The FileOutputStream is a type of the output stream that is used in Java to write data to a file. This class is used when the programmer has to write primitive values into a file. Both byte-oriented and character-oriented data can be written by using this class. However, the FileWriter is more preferred than the FileOutputStream class to write the character-oriented data.

2. ByteArrayOutputStream

In Java, the ByteArrayOutputStream Class writes common data into various files. In this stream, the data gets written into a byte array that can be written to various streams in the future. It holds a copy of data and then forwards it to various streams. The buffer of the ByteArrayOutputStream automatically grows based on the data.

3. FilterOutputStream

The Java FilterOutputStream Class is not used individually as it provides various subclasses to add more functionality. BufferedOutputStream and DataOutputStream are some of the subclasses that this class uses in order to implement the OutputStream class.

3.1 DataOutputStream

The function of the DataOutputStream Class in Java is to permit an application to write primitive Java data types to the output stream in a way that is not dependent on the machine. This class is normally used by the Java to write data which can be read by a data input stream in the future.

3.2 BufferedOutputStream

The main function of the BufferedOutputStream class is to buffer an output stream. It internally makes the use of the buffer in order to store data which adds more efficiency than to write data into the stream directly. This makes the performance of the program better.

3.3 PrintStream

In Java, the PrintStream Class is used to provide methods that write data to another stream. The methods of this class do not throw IOException and there is no requirement to invoke the flush() method because this class flushes the data automatically.

4. PipedOutputStream

PipedOutputStream class should be connected to a PipedinPutStream. So data can be written using PipedOutputStream and can be written using PipedInputStream class.

5. ObjectOutputStream

In Java, the ObjectOutputStream class writes the primitive data types and the objects to an output stream. Such objects that support the serializable interface can only be written to the stream.

Leave a Reply

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