Java to record program logs with files, and java to output streams

Source: Internet
Author: User

Java to record program logs with files, and java to output streams

The out and error types in System are all final and cannot be modified. However, you can set a new output stream through setOut () to write logs.

Import java. io. printStream; import java. io. fileNotFoundException; public class RedirectOutputStream {public static void main (String arg []) {try {PrintStream out = System. out; // retain the original output stream PrintStream ps = new PrintStream (". /log.txt "); // create the file output stream System. setOut (ps); // set to use the new output stream int age = 18; System. out. println ("age variable defined successfully, initial value: 18"); String sex = "female"; System. out. println ("gender variable defined successfully, initial value: female"); String info = "this is "+ Sex +" children, should have "+ age +" years old "; System. out. println ("integrate two variables as info string variables and the result is" + info); System. setOut (out); // restore the original output stream System. out. println ("the program is running successfully. Check the log file. ");} Catch (FileNotFoundException e) {e. printStackTrace ();}}}

The log is as follows:

The age variable is defined successfully. The initial value is 18.
The gender variable is defined successfully. The initial value is female.
Integrate the two variables into the info string variable. The result is that this variable is a girl and should be 18 years old.

Summary:

This method saves the out member variable of the System class as a temporary variable, creates a new file output stream, and sets the output stream as the new output stream of the System class. And output continuous debugging information to the log. Main Methods:

1. setOut () method: reassign the standard output stream of the System class.
2. setErr () method: reassign the standard error output stream of the System class.
3. setIn () method: reassign the standard input stream of the System class.

Related Article

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.