Redirection of Input/Output

Redirection of input

Redirection of input causes the file filename to be opened for reading, and the information stored in this file will be read by the console.readLine() method (or by console.nextLine() if you use the class Scanner for input). The general format for redirection of input is

java Java_bytecode < filename

Redirection of output

Redirection of input causes the file filename to be opened for writing. Now the System.out.print and System.out.println methods will produce the output not to the screen, but to the file filename. The general format for redirection of output is

java Java_bytecode > filename

If the file filename exists, it will be overwritten. However, if the instruction

java Java_bytecode >> filename

will be used instead, then the new output will be appended to the file filename (if it exists).

Redirection of input and output

One can combine both forms of the input/output redirection. Thus, the instruction

java Java_bytecode < filename1 > filename2

causes reading the input from file filename1 and writing the output to file filename2. In particular, the instruction

java Java_bytecode <> filename

will open file filename for input and output.