Overview of Java I/O

1. Overview

In this article, we will learn the basics of Java I/O, an overview of different I/O Streams from the java.io package, overview of the classes in the Java IO (java.io) package and Java IO Purposes and Features.

Key points to know
  • Java I/O (Input and Output) is used to process the input and produce the output.
  • Java uses the concept of the stream to make I/O operations fast. The java.io package contains all the classes required for input and output operations.
  • We can perform file handling in java by Java I/O API.
  • A stream is a sequence of data. In Java, a stream is composed of bytes. It's called a stream because it is like a stream of water that continues to flow.
Let's first focuses on I/O Streams, a powerful concept that greatly simplifies I/O operations.

2. I/O Streams

  • Character Streams - handle I/O of character data, automatically handling translation to and from the local character set.
  • Buffered Streams - optimize input and output by reducing the number of calls to the native API.
  • Data Streams - handle binary I/O of primitive data type and String values.
Let's understand the basics of I/O Streams with diagrams and examples.

3. Working of I/O Streams

An I/O Stream represents an input source or an output destination. A stream can represent many different kinds of sources and destinations, including disk files, devices, other programs, and memory arrays.

Streams support many different kinds of data, including simple bytes, primitive data types, localized characters, and objects. Some streams simply pass on data; others manipulate and transform the data in useful ways.

No matter how they work internally, all streams present the same simple model to programs that use them: A stream is a sequence of data

A program that needs to read data from some source needs an InputStream or a Reader. A program that needs to write data to some destination needs an OutputStream or a Writer. This is also illustrated in the diagrams:

A program uses an input stream to read data from a source, one item at a time:
Reading information into a program.
A program uses an output stream to write data to a destination, one item at time:
Writing information from a program.

In upcoming articles, we'll see streams that can handle all kinds of data, from primitive values to advanced objects.

The data source and data destination pictured above can be anything that holds, generates, or consumes data. Obviously, this includes disk files, but a source or destination can also be another program, a peripheral device, a network socket, or an array.

4. Overview Java IO (java.io) Package

OutputStream class

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 complete hierarchy of OutputStream is available on JavaDoc

InputStream class

InputStream class is an abstract class. It is the superclass of all classes representing an input stream of bytes. The complete hierarchy of InputStream is available on JavaDoc.

5. Java IO Purposes and Features

Java IO contains many subclasses of the InputStream, OutputStream, Reader and Writer classes. The reason is, that all of these subclasses are addressing various different purposes. That is why there are so many different classes. The purposes addressed are summarized below:
  • File Access
  • Network Access
  • Internal Memory Buffer Access
  • Inter-Thread Communication (Pipes)
  • Buffering
  • Filtering
  • Parsing
  • Reading and Writing Text (Readers / Writers)
  • Reading and Writing Primitive Data (long, int etc.)
  • Reading and Writing Objects

6. Java IO Class Overview Table

Having discussed sources, destinations, input, output and the various IO purposes targeted by the Java IO classes, here is a table listing most (if not all) Java IO classes divided by input, output, being byte based or character based, and any more specific purpose they may be addressing, like buffering, parsing etc.

7. Conclusion

In this article we have learned the basics of Java I/O, an overview of different I/O Streams from a java.io package, an overview of the classes in the Java IO (java.io) package, Java IO Purposes and Features and I/O classes overview via a table.

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours

Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course