Input and output-redirect standard inputs and outputs

Source: Internet
Author: User

    • 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.