Mostly from the keyboard.
A program that reads input from the keyboard can also read input from a text 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
reads characters from the keyboard and echoes
them to the monitor.
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, its input can be connected to a disk file:
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 which then uses it as input in place of the keyboard.
The file contained the characters
This is text from the file.
Notice that all the program's output is sent to the monitor, including the (now useless) prompt.
The file input.txt must exist before the program is run.
It could have been created with a text editor.
Is input redirection a feature of Java or a feature of the operating system?