Concepts / Standard Input and Output in Java

Standard Input and Output in Java

The TextIO class is a class in the textio package that contains static methods for input and output operations.

  • Programming

Why Reading and Printing Feel Clumsy

Reading input and printing output are among the first things nearly every program does. A program asks a user for a value, works with it, and reports a result. The standard Java tools for these jobs can feel clumsy to a beginner. The TextIO class exists to make these tasks easier.

Because TextIO looks like a convenient helper, it is tempting to assume it will behave the same way no matter where the program runs. That assumption is the one this article examines. The source material shows that it is not quite true, and the reason tells you something important about how standard input and output depend on their surroundings.

What do you think happens?

Before reading on: the same TextIO-based program is run once in a command-line window and once inside Eclipse. Will the two runs behave the same way?

Reveal answer
Answer:

Not necessarily. TextIO is described as useful in a command-line environment, and it may be inconvenient in integrated development environments such as Eclipse. Its behavior depends on where the program runs.

Standard input does not work particularly well inside an IDE like Eclipse, so the same code can feel and behave differently from one environment to the other.

Where TextIO Lives and How It Is Used

Definition

The TextIO class is a class in the textio package that contains static methods for input and output operations.

A package is a grouping of related classes. The TextIO class belongs to the textio package, so a program that uses it refers to that package to find the class. Because TextIO's methods are static, they are called on the class itself rather than on an object you create first. This is the reason TextIO can be used as a simple utility: you call its input and output methods directly.

Your programcalls TextIO directlytextio packagecontains the classTextIO classstatic methodsInput methodsread valuesOutput methodsprint valuesusescontainsprovidesprovides
How do TextIO and its static methods relate to the textio package and to the calling program?

Tracing a Value from the Keyboard to the Screen

The easiest way to understand TextIO is to picture it as a middle layer between a program and the places its data comes from and goes to. Input can arrive from standard input or from other sources such as files. The program receives those values through TextIO's input methods, works with them, and then sends results back out through TextIO's output methods to standard output or to other destinations.

Follow one value through the path. Text typed by the user enters through standard input, which is the stream called System.in. TextIO's input methods take that text in. The program then stores it in a variable and uses it. When the program prints a result, TextIO's output methods send that text out to standard output, the stream called System.out, which is where it appears on the screen.

Keyboard typinguser inputSystem.instandard input streamTextIO input methodstake in the textProgram variableholds the valueTextIO output methodssend the result outSystem.outstandard output streamScreenresult shownentersread bystored inprinted bywrites todisplayed on
When the program calls a TextIO input method, what path does typed text take to reach a variable, and how does an output call reach the screen?

Wrapping the Standard Streams and Beyond

The main purpose of TextIO is to make the standard input stream, System.in, easier to use. It also provides methods for printing to System.out. Every command-line Java program uses these two standard streams, and TextIO wraps them so that reading a value from the user and printing a result requires less effort.

TextIO is not limited to the standard streams. The source states that it also supports other input sources and output destinations, such as files. This means the same family of TextIO methods can be pointed at a file instead of the keyboard or the screen. The program's logic for working with the values does not have to change just because the source or destination changed.

TextIO methodsone family of methodsSystem.inkeyboard inputInput filefile as sourceSystem.outscreen outputOutput filefile as destinationreads fromreads fromwrites towrites to
How can one set of TextIO methods read from the keyboard or from a file, and what changes in the data path when the source switches?

Only the source end of the path changes when you switch from System.in to a file: the values now come from the file rather than from the keyboard, and the program receives them through the same kind of TextIO input call. The source does not describe the exact file-handling steps, so the practical details should be checked in the TextIO material itself.

Same Program, Two Environments

The central point of this article is that TextIO's behavior depends on where the program runs. The source describes the class as useful in a command-line environment. It also says that integrated development environments such as Eclipse are places where standard input does not work particularly well, and that TextIO might be inconvenient to use there.

Standard inputworks as describedStandard inputdoes not work wellStandard outputuseful for TextIOStandard outputTextIO may be inconvenientsame programsame program
When the same program runs in a terminal versus inside Eclipse, where does typed input come from and where does output go, and why does that change behavior?

Because the environment changes how standard input and output behave, the same TextIO code can produce a different experience. The source does not give the detailed reason, so treat the difference as the documented behavior: TextIO is tied to the command-line setting, and an IDE console may be a less comfortable place to use it. The source points to another file as an alternative for that situation, although it does not name that file in the material provided.

Mistakes Learners Make About TextIO

  • Common mistake

    Assuming TextIO works identically in every environment

    Why it fails

    The source states that TextIO is useful in a command-line environment and may be inconvenient in IDEs such as Eclipse, so its behavior depends on where the program runs.

    Do this instead

    Predict behavior per environment and test in the one you plan to use.

  • Common mistake

    Thinking TextIO is only for the keyboard and screen

    Why it fails

    TextIO also supports other input sources and output destinations, such as files.

    Do this instead

    Remember that the same method family can read from or write to a file.

  • Common mistake

    Treating TextIO as a replacement for System.in and System.out

    Why it fails

    TextIO wraps and simplifies System.in and System.out; the underlying standard streams are still the sources and destinations it works with.

    Do this instead

    Think of TextIO as a layer over the standard streams, not a separate place where input arrives.

  • Common mistake

    Forgetting that the TextIO methods are static, so no object is created before calling them

Predicting Behavior Before You Run

MEDIUM

A friend wrote a short program that uses TextIO to read a value the user types and print a result. They say it will behave exactly the same whether they run it in a terminal or inside Eclipse. Using only what this article describes, explain whether you agree, and name the environment-dependent factor that the source highlights.

Hints
  • Recall which environment the source calls useful for TextIO.
  • Think about the role of System.in in the data path from the keyboard to the variable.
  • Check whether the source says the behavior is identical across environments.
EASY

Sketch the data path for a program that reads a file with TextIO and prints the result to System.out. Label where the values come from, where TextIO sits, and where the output goes.

Hints
  • The source is the file instead of the keyboard.
  • TextIO sits between the source and the program, and between the program and the output.

Key Takeaways

  • TextIO is a class in the textio package that contains static methods for input and output operations, so its methods are called directly on the class.
  • TextIO makes the standard input stream, System.in, easier to use and provides methods for printing to System.out.
  • TextIO also supports other sources and destinations, such as files, using the same family of methods.
  • TextIO is described as useful in a command-line environment and may be inconvenient in IDEs such as Eclipse, so its behavior depends on where the program runs.