---
title: "Java I/O: Input and Output Streams"  
description: "One of the important set of classes comprised in Java API is the java.io package. This is one of the core packages of the Java language and was a part"  
author: "zack mathews"  
published: 2016-05-27  
updated: 2018-03-15  
canonical: https://www.mindstick.com/blog/11131/java-i-o-input-and-output-streams  
category: "java"  
tags: ["java"]  
reading_time: 3 minutes  

---

# Java I/O: Input and Output Streams

One of the important set of classes comprised in Java API is the **java.io** package. This is one of the core packages of the [Java language](https://www.mindstick.com/interview/33712/what-is-jdk-jre-and-jvm-in-java-language) and was a part of [JDK 1.0](https://www.mindstick.com/blog/11082/jdk-1-0-code-name-oak). These classes facilitate the input/output functionality in our programs.

**Typical examples of such functionalities include:**\

- Reading from the keyboard

- Sending some output to the console

- Storing data in a disk file

- Chatting with another user using peer-to-peer networking

- [Transferring files](https://answers.mindstick.com/qa/30344/why-transferring-files-between-android-to-iphone-a-complextask)

- Browsing the Web, and so on.

Thus, the I/O classes are used in a wide range of applications, including the latest innovations such as voice and video calls, peer-to-peer gaming, and more.

##### Input/Output Streams

We may not have realized this, but we have already used some functionality of I/O classes in the previous examples. In many of our programs, we used **System.out.println** to output a message to the user console.

**We learned earlier that:**

- println is a method executed on the out object.

- The out object is of type OutputStream, which is an [abstract class](https://www.mindstick.com/articles/1430/abstract-class). It is the [superclass of all classes](https://www.mindstick.com/interview/33847/which-class-is-a-superclass-of-all-classes) representing an output stream of bytes.

- A stream accepts bytes and sends them to some sink

Likewise, to read [input from the user](https://www.mindstick.com/forum/158952/what-are-the-different-methods-for-reading-input-from-the-user-in-python), we used the **System.in.read method.**

- The in is an object of type InputStream.

- Both InputStream and OutputStream belong to the family of I/O classes.

- The System class, which is defined in the [java.lang package](https://www.mindstick.com/interview/2725/do-i-need-to-import-java-lang-package-any-time-why), contains three static fields, called in, out, and err.

- The in is of type InputStream, whereas out and err are of the PrintStream type, which is a subclass of OutputStream.

Java defines the functionality of its various I/O classes through streams. A stream is an abstraction and can be [thought of as](https://answers.mindstick.com/qa/39521/who-thought-of-as-a-pioneer-of-economic-nationalism) a flow of data from a source to a sink.

##### A stream can be classified in two ways.

- A source stream, also called an input stream, initiates the flow of data.

- A sink stream, also called an output stream, terminates the flow of data.

Source and sink streams are also called node streams. A stream is just a continuous flow of data. Like an array that holds some data, a stream does not have the concept of a data index. We cannot move back and forth in a stream. The data can only be accessed sequentially.

A stream either consumes or provides information. A stream is usually linked to a physical device. It provides a uniform interface to a device for data flow. In the case of an input stream, the device to which it connects may be a [physical disk](https://answers.mindstick.com/qa/50283/how-many-logical-drive-is-it-possible-to-fit-on-to-a-physical-disk), a [network connection](https://answers.mindstick.com/qa/94984/how-do-you-change-the-network-connection-priority-in-windows-10), a keyboard, and so on.

In the case of an output stream, it may be connected to a console, a physical disk, a network connection, and so on. Thus, when we use the input/output stream classes, our program code becomes independent of the device to which the stream connects. Examples of source streams are files and memory buffers. A printer or a console can represent a stream destination.

##### The streams in Java are of two types:

- Byte oriented

- Character oriented

The byte streams operate on bytes of data, whereas the character-oriented streams operate on characters, typically a Unicode character set. JDK 1.0 provided only byte-oriented streams. JDK 1.1 introduced character-oriented streams. Because the underlying mechanism for streams is still byte oriented, JDK 1.1 also introduced bridge classes to convert a byte stream into a character stream, and vice versa.

---

Original Source: https://www.mindstick.com/blog/11131/java-i-o-input-and-output-streams

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
