1. Input redirection
Command line: Java [Java class file] < [input file path name]
Code: InputStream inputstream = new FileInputStream (
"[Input file path name]");
System.setin (InputStream);
Scanner Scanner = new Scanner (system.in);
2. Output redirection
Command line: Java [Java class file] > [Output file path name]
Eclipse:run-->run configurations-->common--> tick file, enter [output file path name]
Code: PrintStream printstream = new PrintStream (
"C:/users/administrator/desktop/output.txt");
System.setout (PrintStream);
3. Resume Redirection
After the redirection function is not required, execute the following statement:
1) Restore "standard" error output stream
FileOutputStream fderr = new FileOutputStream (FILEDESCRIPTOR.ERR);
System.seterr (New PrintStream (New Bufferedoutputstream (Fderr, +), true));
2) Restore "standard" input stream
FileInputStream Fdin = new FileInputStream (filedescriptor.in);
System.setin (New Bufferedinputstream (Fdin));
3) Restore "standard" output stream
FileOutputStream fdout = new FileOutputStream (filedescriptor.out);
SetOut0 (New PrintStream (New Bufferedoutputstream (fdout, +), true));
In my opinion, unless forced, it is best not to use method two, unless you are confident, otherwise cautious as well!
Java I/O redirection