INPUT REDIRECTION
Demo for input from the input file. Compile InputDemo.java in a usual way using
>javac InputDemo.java
Create an input file indat.txt with a list of words. Place each word on a separate line. Save file indat.txt in the same directory where you saved InputDemo.java. To redirect the input run the program as:
>java InputDemo <indat.txt
Instead of seeking input from the keyboard it will take input from the file indat.txt and send output to standard output.
import java.io.*;
class InputDemo{
public static void main(String[] args) throws IOException{
BufferedReader r = new BufferedReader
(new InputStreamReader(System.in));
String s;
while(true){
s = r.readLine();
if (s == null)
return;
System.out.println(s);
}// for
} // main
}// class
OUTPUT REDIRECTION
When the same program is compiled and run as
>java InputDemo >outdat.txt
This time it will seek input from the keyboard, but output will be redirected into output file outdat.txt.
Since infinite loop should be terminated after you finish entering several words press CTRL/Z to stop.
INPUT AND OUTPUT REDIRECTION
When the same program is compiled and run as
>java InputDemo <indat.txt >outdat.txt
This time it will seek input from the file indat.txt, and output will be redirected into output file outdat.txt.