MT2 - Redirection and Piping
There are three main operators you need to know:
Symbol | Name | Purpose |
|---|---|---|
| Input redirection | Get input from a file |
| Output redirection | Send output into a file |
| Piping | Send one program's output into another program |
We'll go through each one, then combine them.
Input Redirection (<)
Normally, when a Java program uses StdIn, it reads input from the keyboard/input stream.
But you can redirect that input from a file instead.
Imagine a file called numbers.txt containing:
10
20
30
40
Java program:
public class Sum {
public static void main(String[] args) {
int sum = 0;
while (!StdIn.isEmpty()) {
int num = StdIn.readInt();
sum += num;
}
StdOut.println(sum);
}
}
normally you run:
java Sum
With input redirection:
java Sum < numbers.txt
What's happening?
numbers.txt
10, 20, 30, 40
< input redirection
Java program
Reads the numbers through StdIn
Output: 100
Output Redirection ( > )
Now let's reverse the direction.
Suppose your program calculates the sum and prints:
StdOut.println(sum);
Normally, the output appears in your terminal.
But what if you want to save the output into a file?
java Sum > result.txt
The > operator sends the program's standard output into result.txt instead of displaying it in the terminal.
So result.txt receives output 100
Important distinction: > vs. >>
This is a common thing to watch for.
Operator | What happens |
|---|---|
| Creates or overwrites the file |
| Appends output to the end of the file |
Example:
Suppose result.txt already contains:
50
Running:
java Sum > result.txt
Replaces old output with new output
Running:
java Sum >> result.txt
Adds the new output to 50
Combining input and output redirection
you can use both operators in one command
java Sum < numbers.txt > result.txt
Read this from left to right:
< numbers.txt: Get input fromnumbers.txt.Run
Sumand calculate the answer.> result.txt: Save the output inresult.txt.
So the program reads from one file and writes to another.
You should absolutely recognize this command on the exam.
PIPINGGGGG
Piping ( | ) takes the output of one program and makes it the input of another program
Think of it as passing information directly between two programs without needing to save it in a file.
How it works:
Imagine we have two Java programs
Program A:
public class ProgramA {
public static void main(String[] args) {
StdOut.println(10);
StdOut.println(20);
StdOut.println(30);
}
}
Program B:
public class ProgramB {
public static void main(String[] args) {
int sum = 0;
while (!StdIn.isEmpty()) {
sum += StdIn.readInt();
}
StdOut.println(sum);
}
}
Normally, Program A prints:
10
20
30
Program B reads numbers and adds them.
But what if we want Program B to receive Program A’s output automatically?
We use:
java ProgramA | java ProgramB
Here's what happens:
Program A
Prints 10, 20, 30
Pipe (|)
Output becomes input
Program B
Reads and adds the numbers
Output: 60
Notice something important:
Program A uses StdOut to produce info
Program B uses StdIn to receive info
Piping Multiple Programs
You can connect more than 2 programs
For example:
java ProgramA | java ProgramB | java ProgramC
The information flows from left to right:
Program A produces output.
Program B receives A's output, processes it, and produces new output.
Program C receives B's output.
Each program receives the previous program's standard output as its standard input.
Combining Piping with Redirection
You can also combine pipes with files.
Example:
java ProgramA < input.txt | java ProgramB > output.txt
Read it from left to right:
Program A gets its input from
input.txt.Program A processes that input.
Program A's output is piped into Program B.
Program B processes that information.
Program B's output is saved in
output.txt.