Java I/O TutorialLast Updated : 22 Jan 2026 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. What is Java I/O?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. Core Concepts of Java I/OJava I/O is based on two main concepts: 1. StreamsA 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:
2. Readers/WritersReaders and Writers are specially designed for handling character-based data.
They provide a convenient way to work with text data. What is a Stream in Java?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 ClassOutputStream 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. Methods of OutputStream ClassThe following table lists the useful methods of OutputStream class:
OutputStream HierarchyThe following image shows the class hierarchy of the OutputStream, illustrating its subclasses and their relationships used for writing byte data to different destinations. ![]() Example of OutputStream ClassThe following example demonstrates how a student writes exam answers to a file using the OutputStream class. Output: Data written successfully InputStream ClassInputStream class is an abstract class. It is the superclass of all classes representing an input stream of bytes. Useful Methods of InputStream ClassThe following table lists the useful methods of InputStream class:
InputStream HierarchyThe 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. ![]() Example of InputStream ClassThe following example demonstrates how a student reads notes from a file using the InputStream class. Output: Student exam answers OutputStream Vs. InputStreamThe 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 I/O ClassesJava 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.
Next TopicJava FileOutputStream class |
We request you to subscribe our newsletter for upcoming updates.