Java I/O (Input and Output) is used to read data from input sources and write data to output destinations.
In this chapter, we will learn the basics of Java I/O, core concepts like streams and readers/writers, important I/O classes, and best practices for handling input and output operations.
Java I/O refers to the process of handling input and output operations in Java applications. It is mainly used to read data from sources such as the keyboard or files and write data to destinations like the console or files.
Java uses the concept of streams to make input and output operations efficient. All Java I/O-related classes are available in the java.io package.
Java I/O is also widely used for file handling in Java.
Java I/O is based on two main concepts:
A stream represents a continuous flow of data. In Java, streams are used to read or write data sequentially.
There are two types of streams:
Streams are further classified into:
Readers and Writers are specially designed for handling character-based data.
They provide a convenient way to work with text data.
A stream is a sequence of data bytes. It is called a stream because data flows continuously, similar to a stream of water.
Java automatically creates three standard streams, which are connected to the console:
Let's see the code to print output and an error message to the console.
Let's see the code to get input from console.
OutputStream class is an abstract class. It is the superclass of all classes representing an output stream of bytes. An output stream accepts output bytes and sends them to some sink.
The following table lists the useful methods of OutputStream class:
| Method | Description |
|---|---|
| public void write(int) throws IOException | It is used to write a byte to the current output stream. |
| public void write(byte[])throws IOException | It is used to write an array of byte to the current output stream. |
| public void flush() throws IOException | It flushes the current output stream. |
| public void close() throws IOException | It is used to close the current output stream. |
The following image shows the class hierarchy of the OutputStream, illustrating its subclasses and their relationships used for writing byte data to different destinations.

The following example demonstrates how a student writes exam answers to a file using the OutputStream class.
Output:
Data written successfully
InputStream class is an abstract class. It is the superclass of all classes representing an input stream of bytes.
The following table lists the useful methods of InputStream class:
| Method | Description |
|---|---|
| public abstract int read() throws IOException | It reads the next byte of data from the input stream. It returns -1 at the end of the file. |
| public int available() throws IOException | It returns an estimate of the number of bytes that can be read from the current input stream. |
| public void close() throws IOException | It is used to close the current input stream. |
The following image shows the class hierarchy of the InputStream in Java, illustrating its subclasses and their relationships used for reading byte data from different sources.

The following example demonstrates how a student reads notes from a file using the InputStream class.
Output:
Student exam answers
The OutputStream and InputStream classes are used to write data to and read data from different sources and destinations.
An OutputStream is used by a Java application to write data to a destination. The destination can be a file, a byte array, a peripheral device, or a network socket. Whereas, an InputStream is used by a Java application to read data from a source. The source can be a file, a byte array, a peripheral device, or a network socket.
The following image shows the working and relationship of the OutputStream and InputStream classes.

Java provides a rich set of classes to perform input and output operations. These classes help programs read data from different sources and write data to various destinations.
We request you to subscribe our newsletter for upcoming updates.