Both of the other two answers above are partially correct, but partially misleading.
Using the following syntax...
java ReadInput < input.txt
...the actual command that is run on the java binary is:
java ReadInput
The operating system shell interprets the <
sign, and sends the contents of input.txt to the Standard Input Stream (System.in).
When you call System.in.readLine()
, the code checks whether there is a line of input available from the Standard Input Stream. When you piped in a file, this means it takes the next line of the file (as if the OS were taking the contents of the file and typing them in at the prompt). When you run the program without piping a file, it will wait until you, the user, provide it with a line of input from the shell and press the return key.
Command-line arguments to the JVM work differently, and do not use the Standard Input Stream (System.in). Ex:
java ReadInput "Input number one" "Input number two"
In this case, you are passing in two command-line arguments. These properties will be available through the args
array:
public static void main(String[] args) throws IOException {
System.out.println(args.length); //3
System.out.println(args[0]); //"ReadInput"
System.out.println(args[1]); //"Input number one"
System.out.println(args[2]); //"Input number two"
}
With the code that you provided, the program will end when the result of a readLine() call returns null
. I believe you may be able to just push enter at the prompt to send a blank line, and end the program. If not, you may have to fix the program to check for an empty string (input.readLine().equals("")).
Hope this helps.