Concepts / Input and Output Readers and Writers in Java

Input and Output Readers and Writers in Java

I/O is the interaction of a program with the rest of the world, and it is essential for any useful program.

  • Programming

A Program With No Way to Communicate

Imagine a program that computes an answer but has no way to receive a number from a user, show a result on a screen, or save anything for later. However clever its internal logic, nobody can use it. Programs become useful only when they can exchange information with the world around them.

This exchange is called input and output, usually shortened to I/O. Input is information arriving at the program. Output is information leaving it. Every useful program, from a small calculator to a large server, depends on I/O in some form.

What do you think happens?

When a program displays a result on the screen, is that input or output?

  • Input

  • Output

Reveal answer
Answer:

Output

Output is information leaving the program. Showing a result on a screen sends data from the program out to a device, so it is output.

Why Many Devices Make I/O Hard

A computer can be connected to many different kinds of input and output devices. Each kind may behave differently. If a programming language handled every device as its own special case, the amount of complexity would quickly become overwhelming. The source notes that designing good facilities for input and output has historically been one of the hardest parts of programming language design.

Consider what a program would need to know if it had to handle each device directly: the particular way a keyboard reports keystrokes, the way a disk stores bytes in a location, and the way a network connection delivers data. Writing separate logic for every one of these would tie the program tightly to hardware details.

Abstraction as the Way Out

Definition

An abstraction is a simplified way of representing a device so that the program does not need to know every detail of the hardware behind it. The source describes finding good abstractions for I/O devices as one of the major achievements in the history of programming.

I/O abstractions let a language handle many device types without treating each as a special case. Once the program talks to a common representation, the same kind of read or write request can be used whether the other end is a keyboard, a file, or a network connection. The details of each device are handled behind that representation.

What an I/O Stream Is

Definition

In Java, I/O streams are the main abstraction for input and output. Each stream is either a source of input or a destination for output.

Tracing the Path of Data

Because each stream has a direction, you can trace how data moves through a program. Input flows from a device, through an input stream, into the program. Output flows the other way: from the program, through an output stream, to a device. The program sits at the center because it is the party that consumes input and produces output.

Input deviceInput streamProgramOutput streamOutput devicedata arrivesinput into programoutput from programdata leaves
For a given stream, which way does data move relative to the program, and which side is the source and which is the destination?

Read the diagram as two paths sharing one program. The upper path carries input inward, and the lower path carries output outward. The same program can have both kinds of path at once, and each path is governed by the direction of its own stream.

One Interface, Many Endpoints

The main benefit of the stream abstraction is that the program's logic can stay the same while the source or destination changes. The diagram below shows a single program connected to input and output streams, and each stream connected to a different kind of endpoint.

Input streamKeyboardProgramFileOutput streamNetwork connectionScreenreads fromwrites tosourcesourcesourcedestinationdestination
How can the same read or write code work whether the data comes from a keyboard, a file, or a network connection?

Notice that the program talks to a stream, not to the keyboard or the file directly. Whichever endpoint sits behind the stream, the program's side of the exchange is expressed in the same terms: read from an input stream, or write to an output stream.

Deciding Input or Output for a Given Stream

Worked example

Classifying the streams in a simple program

A program asks a user for a number, computes the square of that number, displays the result on the screen, and saves the result to a file for later. Identify each stream as an input stream or an output stream.

  1. 1
    Identify the data that arrives at the program

    The number typed by the user arrives at the program. Information arriving at the program is input, so the stream carrying the typed number is an input stream.

  2. 2
    Identify the data that leaves the program for the screen

    The squared result is shown on the screen. Information leaving the program is output, so the stream feeding the screen is an output stream.

  3. 3
    Identify the data that leaves the program for the file

    The result is saved to a file. The file is a destination, so the stream carrying the result there is an output stream.

  4. 4
    Check the direction against the program

    In every case, ask whether data is entering or leaving the program. Entering means input, leaving means output. The program itself is never the device; it is the consumer of input and the producer of output.

Result

The keyboard stream is an input stream. The screen stream and the file stream are both output streams.

Streams, Files, and Channels

Streams are not the only I/O abstraction. Files and channels also exist, and they serve the same general purpose: simplifying work with different kinds of devices. The source states that it will concentrate only on streams in this section, so the remainder of this article treats streams as the focus while acknowledging the other abstractions around them.

I/O abstractionsStreamsfocus of this articleDirection: input oroutputFilesChannelsincludesincludesincludeseach has a direction
What contains what, and how do a stream, a file, and a channel relate to one another?

The concept map places streams, files, and channels side by side as different I/O abstractions. Each is a way of working with devices without treating every device as a special case. The source does not describe the internal relationship between files and channels in detail, so this article does not make claims beyond the fact that both are I/O abstractions alongside streams.

Mistakes Beginners Make With Streams

  • Common mistake

    Treating a stream as if it were the device itself

    What it looks like

    Saying the stream is the keyboard, or that the stream is the file on disk.

    Why it fails

    The source defines a stream as the program's handle on a device, expressed in terms of direction. It is not the device.

    Do this instead

    Describe a stream as a handle with a direction, and name the device separately as its source or destination.

  • Common mistake

    Assuming every stream moves data in both directions

    What it looks like

    Believing one input stream can also be used to send results out of the program.

    Why it fails

    Each stream is either a source of input or a destination for output. Direction is part of what a stream is.

    Do this instead

    Decide the direction first. Use an input stream to bring data in and a separate output stream to send data out.

  • Common mistake

    Judging direction by the device instead of the program

    What it looks like

    Calling the screen an input device because a user looks at it, so the stream to it must be input.

    Why it fails

    Direction is defined relative to the program. Output is information leaving the program, regardless of who reads the screen.

    Do this instead

    Ask whether data is entering or leaving the program. Data leaving the program means an output stream.

  • Common mistake

    Believing the stream abstraction only works for one kind of device

Practice Classifying Streams

EASY

A program reads a sequence of values sent by another computer over a network connection, sorts them, and writes the sorted list to a file. For each of the two streams involved, state whether it is an input stream or an output stream, and explain your reasoning in terms of the program.

Hints
  • Ask whether the data is entering or leaving the program.
  • The network stream delivers values to the program, and the file stream receives values from the program.
  • Direction is defined relative to the program, not to the device on the other end.
MEDIUM

Explain, in your own words, why a stream is described as an abstraction rather than as a device. Use the phrase handle on a device in your answer.

Hints
  • Think about what the program needs to know in order to read or write.
  • Consider how the same read or write request could apply to several kinds of device.

Key Takeaways

  • I/O is the interaction of a program with the outside world. Input is information arriving at the program, and output is information leaving it.
  • Java's main I/O abstraction is the stream, which is a program's handle on a device expressed in terms of direction.
  • Each stream is either a source of input or a destination for output, and direction is always judged relative to the program.
  • Streams let the program work with many device types through one representation, and files and channels are other I/O abstractions that serve the same purpose.