(JAVA) zero-based-print stream PrintStream log file

Source: Internet
Author: User

The logging here is implemented using the print stream.

The content in the text message is of type string. And like writing data in a file, we often use the file output stream object FileOutputStream.

1 New File ("F:\\a.txt"); 2 New FileOutputStream (file,true); // The second argument is an append text 3 outputstream.write (97);

After the above code executes, the content in A.txt is a, because the write method receives data of type Byte, and 97 corresponds to an ASCII code of a.

Suppose I wanted to write 97 to a file? Then the third line of code should be changed to

1 outputstream.write ("the". GetBytes ()); // convert 97 as a string to a byte array first

And PrintStream to appear, yes we write data into the file becomes very convenient, what you pass in, will give you write what data. The reason is that he helped us change lives internally.

New File ("F:\\a.txt"); New printstream (file); 3 printstream.println (); 4 printstream.println (' a '); 5 printstream.println ("Hello World"); 6 Printstream.println (true); 7 printstream.println (3.14); 8 Printstream.println (new

The result from the above code is:

Visible regardless of the data type, it is converted to a string, even the object is no exception.

Here's another piece of code to learn about Java: System.out.println (); Out in the code, a PrintStream object in the System class, called the standard output stream object. The standard output stream object prints data to the console. Consult the API to see the following methods,

static void SetOut (printstream out)//reassign "standard" output stream .

The output stream object can be re-specified, and the output of System.out.println () will be printed to the place where we want to print it.

1 New File ("F:\\a.txt"); 2 New printstream (file); 3 system.setout (printstream); 4 System.out.println ("Print to F:\\a.txt");

This time the content is written back to the file a.txt instead of being printed in the console.

Finally, this paper focuses on the preservation of log information.

Suppose you have code:

1 Try {2    int n = 5/0; 3 }catch(Exception e) {4   e.printstacktrace (); 5 }

The execution result throws the error log information we want.

Java.lang. arithmeticexception:/by zero
At log. Demolog.main (demolog.java:26)

What do you do when you want to save the log information?

Seeing these 3 overloaded methods in the exception class, it is not difficult to know that we can save the log information to where we want it, just by assigning him a printout stream object.

 1  File File = new  File ("F:\\a.log"  2  printstream printstream = new   PrintStream (file);  3  {< /span>4  int  n = 5/0;//  5 }catch   (Exception e) { 6   E.printstacktrace (printstream);  7 }  

The above code executes repeatedly,

However, the logged log information is always logged only one. This is obviously not what we want, log information, can not only record one bar? So what's the use of being there?

In fact, the decision to append text information is not e.printstacktrace (PrintStream); method, the key point is the flow object,

A visible print stream object is an incoming object that has an OutputStream interface as a parameter. Since it is an interface, it is not possible to new out of the OutputStream object, you can use his subclass FileOutputStream object as a parameter passed in. Also, the FileOutputStream stream can be appended,

New FileOutputStream (file,true);//The second argument is an append text

At this point, it is passed as a parameter, and the PrintStream stream can naturally append the content.

1File File =NewFile ("F:\\a.log");2PrintStream PrintStream =NewPrintStream (NewFileOutputStream (file,true),true);3         Try{4             intn = 5/0;//Divisor is zero exception5}Catch(Exception e) {6 E.printstacktrace (printstream);7}

After you have executed the code 3 times:

You can see that the log information is saved with 3 entries, log information records are saved for the purpose of achieving!

(JAVA) zero-based-print stream PrintStream log file

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.