Java I/O redirection

Source: Internet
Author: User

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

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.