What is Java I/O?

Here, you will know several interfaces provided by the java.io package.

What is Java I/O?

What is Java I/O?

     

Introduction

The Java Input/Output (I/O) is a part of java.io package. The java.io package contains a relatively large number of classes that support  input and output operations. The classes in the package are primarily abstract classes and stream-oriented that define methods and subclasses which allow bytes to be read from and written to files or other input and output sources. The InputStream and OutputStream  are central classes in the package which are used for reading from and writing to byte streams, respectively.

The java.io package can be categories along with its stream classes in a hierarchy structure shown below:

InputStream:

The InputStream class is used for reading the data such as a byte and array of bytes from an input source. An input source can be a file, a string, or memory that may contain the data. It is an abstract class that defines the programming interface for all input streams that are inherited from it. An input stream is automatically opened when you create it. You cans explicitly close a stream with the close( ) method, or let it be closed implicitly when the object is found as a garbage.

The subclasses inherited from the InputStream class can be seen in a hierarchy manner shown below:

InputStream is inherited from the Object class. Each class of the InputStreams provided by the java.io package is intended for a different purpose.

 

OutputStream:

The OutputStream class is a sibling to InputStream that is used for writing byte and array of bytes to an output source. Similar to input sources, an output source can be anything such as a file, a string, or memory containing the data. Like an input stream, an output stream is automatically opened when you create it. You can explicitly close an output stream with the close( ) method, or let it be closed implicitly when the object is garbage collected.

The classes inherited from the OutputStream class can be seen in a hierarchy structure shown below:

OutputStream is also inherited from the Object class. Each class of the OutputStreams provided by the java.io package is intended for a different purpose.

How Files and Streams Work:

Java uses streams to handle I/O operations through which the data is flowed from one location to another. For example, an InputStream can flow the data from a disk file to the  internal memory and an OutputStream can flow the data from the internal memory to a disk file. The disk-file may be a text file or a binary file. When we work with a text file,  we use a character stream where one character is treated as per byte on disk. When we work with a binary file,  we use a binary stream.

The working process of the I/O streams can be shown in the given diagram.