Input Redirection

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        

Answer:

Mostly from the keyboard.

Input Redirection

A program that reads input from the keyboard can also read input from a file. This is called input redirection, and is a feature of the command line interface of most operating systems. Say that you have a program named echo.java that writes to the monitor what the user has typed. Here is how the program usually works:

c:/> java echo
Enter your input: 
User types this.
You typed: User types this.
c:/>

Without making any changes to the program you can do the following:

c:/> java echo < input.txt
Enter your input:
You typed: This is text from the file.
c:/> 

The "< input.txt" part of the command line connects the file input.txt to the program and uses it as input in place of the keyboard. In this example the file contained the characters "This is text from the file."

The file input.txt must exist before the program is run. Create it with a text editor.

QUESTION 2:

Is input redirection a feature of Java or a feature of the operating system?