Java Reference
In-Depth Information
Note how the error messages are still shown on the console, whereas standard output is now saved
in the output.txt text file (verify this by opening the file). Is there a way to redirect error messages
as well? Indeed there is, by using 2> : java -jar linereverser.jar > output.txt 2> error.
txt . Note that > (standard output redirection) can then also be written as 1> . If you want to append
to the end of a file instead of creating a new one, you can use >> (two arrows) instead of > . Try this
out if you like.
Let us now take this a step further. Create a text file called input.txt on your Desktop containing
the following:
apple
amoreroma
test
aabbaa
No doubt you can see where this is going; we would like to use the contents of this file as input
instead of typing the lines ourselves. Again, this is easy, using the < redirection operator, like so:
java -jar linereverser.jar < input.txt . See Figure 8-9.
fiGureĀ 8-9
Indeed, you can also combine these operators to construct something like this: java -jar liner-
everser.jar < input.txt > output.txt 2> errors.txt .
So far, you've been taking input and sending output from and to files, but it is also possible to redi-
rect the standard streams from one program to another program. To illustrate this, we will use a
standard Windows command-line program called type . This program just shows the contents of a
file on the standard output. Try it out by typing type input.txt . See Figure 8-10.
fiGureĀ 8-10
Search WWH ::




Custom Search