- redirect Standard inputs and outputs
Java's standard inputs and outputs are represented by system.in and System.out, which, by default, represent keyboards and monitors, respectively.
3 methods for redirecting standard inputs and outputs are available in the System class:
Seterr (PrintStream err) reassign the "standard" error output stream.
SetIn (InputStream in) reassign the "standard" input stream.
SetOut (PrintStream out) redistribute the "standard" output stream.
Of course we can write our own program, modify the input and output of our application. For example, now I do not want the standard output to the display, but a file of my own, or I do not need to enter the content from the keyboard, but read my local text.
The specific code is as follows:
Import Java.io.fileoutputstream;import java.io.printstream;/** * * @version 1L * @author Linkinpark * @since 2014-12- * @motto dream like fireworks heart like water, classmate teenager not romance * @desc ^ redirect the standard output stream, speaking System.out output to the specified file instead of the monitor. */public class Linkin{public static void Main (string[] args) {PrintStream PS = Null;try{ps = new PrintStream (New FileOutput Stream ("Src/linkinpark ..."); System.setout (PS); System.out.println ("redirect the standard output here ... "); SYSTEM.OUT.PRINTLN (new Linkin ());} catch (Exception e) {e.printstacktrace ();} FINALLY{IF (PS! = null) {Ps.close ();}}}}
Import Java.io.fileinputstream;import java.io.ioexception;import java.util.scanner;/** * * @version 1L * @author Linkinpark * @since 2014-12-30 * @motto dream like fireworks heart like water, classmate youth not romance * @desc ^ redirect the standard input stream, redirect system.in to the specified file instead of the keyboard input */public class Link In{public static void Main (string[] args) {FileInputStream fis = Null;try{fis = new FileInputStream ("Src/linkinpark ..."); System.setin (FIS);//Use system.in to create Scanner objects to obtain the standard input Scanner sc = new Scanner (system.in); Sc.usedelimiter ("\ n");// Add delimiter while (Sc.hasnext ()) {System.out.println ("The specified file is entered in:" + sc.next ());}} catch (Exception e) {e.printstacktrace ();} Finally{if (FIS! = null) {try{//here also to catch unusual, somewhat disgusting fis.close ();} catch (IOException e) {e.printstacktrace ();}}}}
Input and output-redirect standard inputs and outputs