Course Links

Exercises

Resources

External

Background

Normally, when you run a program in Unix, the input comes from the keyboard and the output is sent to the screen. However, Unix allows you to change this behavior, through what is known as redirection of input and output.

To use output redirection, you must include > outputfile on the command line that runs your program. This will send all the ordinary output from the program to the file called outputfileinstead of the screen. To use input redirection, you include < inputfile. This will take all the input from the file called inputfile, as though you had typed that file's contents on your keyboard.

Exercise

Let's try a few examples. Download and compile Redirect.java. Then run it while redirecting the output to out.txt. We can see what was stored in numbers.txt using the more command.

$ javac Redirect.java
$ java Redirect > numbers.txt
Please enter three integers, one per line.
3
4
5
Printing the same integers, plus one...
$ more numbers.txt
4
5
6

You should note several things about this interaction. First, only the output sent to System.out was redirected to the file. This is deliberate. System.err exists for error messages and other important information that the user may need to see immediately. Second, the output that went to System.out did not appear on the screen. This can be confusing if the user is expecting information (such as a prompt about what to do next) to appear on the screen. This is why we sent the prompts in this program to System.err in this program.

Now let's try input redirection. We can use the file we just created -- numbers.txt -- as input to the program. (Any text file will do, as long as it contains data in the form the program expects.)

$ java Redirect < numbers.txt
Please enter three integers, one per line. Printing the same integers, plus one... 5 6 7

Note that here the prompt asks us to input three integers, but the program doesn't wait for us -- it just reads the integers from the input file. Anything we may have wanted to type as input will be ignored, because System.in is drawing from a file rather than the keyboard. Again, this can be confusing. Often it's a good idea to design a program to work only with redirection, or only with human interaction, and not both. This avoids such complications.

To Do

Write a program called TextCounter.java that counts the number of lines in the input stream, and the number of words. (Hint: Read the file line by line, and use the split method of String to get the number of words in each line.) Use input redirection to make your program count the number of words and lines in the Declaration of Independence.

Further Thoughts

You can use both input and output redirection at the same time. Try it with our simple test program -- take the input from numbers.txt as above, and send the output to numbers2.txt.

Other variations: Normally, you can't redirect output to overwrite a file that already exists; you must delete it first (using rm). However, you can append the output to an existing text file using >> instead of >. Also, if you want to redirect the output of System.err also, you can use >&.