[Java iO] _ print stream notes

Source: Internet
Author: User

[Java iO] _ print stream notes

Objectives of this Chapter
Understanding the operation of the print stream
Master formatting output

Print stream
In the entire Io package, print streams are the most convenient class for output information, mainly including the byte print stream and the character print stream (printwriter ). The print stream provides a very convenient printing function that can print any data type, such as decimal places, integers, strings, and so on.

Here we only introduce the byte print stream (printstream), because the character print stream is similar to the byte print stream, but it is not commonly used.

Review: outputstream was used to print information before, but in this case, all data output is very troublesome, string-> byte [], the output can be easily printed in the stream.

Many print () or println () methods are defined in this class. System. Out. println (), which can print any data type.

Constructor:
Public printstream (outputstream out)-> specifies the output location

This constructor receives subclass of outputstream.

Use printstream to output information.

Import Java. io. *; public class printdemo01 {public static void main (string [] ARGs) throws exception {printstream PS = NULL; PS = new printstream (New fileoutputstream (new file ("D: "+ file. separator + "test.txt"); PS. print ("hello"); PS. println ("world !! "); PS. Print (" 1 + 1 = "+ 2); PS. Close ();}}

In other words, the fileoutputstream class is actually packaged. Such a design is called a decoration design in Java.

Format output

 
Import Java. io. *; public class printdemo02 {public static void main (string [] ARGs) throws exception {printstream PS = NULL; // declare the print Stream object PS = new printstream (New fileoutputstream (new file ("D:" + file. separator + "test.txt"); string name = "Niuer grazing"; int age = 30; float score = 990.356f; char sex = 'M'; PS. printf ("Name: % s; age: % d; score: % F; Gender: % C", name, age, score, sex); PS. close ();}}

Simplified operation:

 
Import Java. io. *; public class printdemo03 {public static void main (string [] ARGs) throws exception {printstream PS = NULL; // declare the print Stream object PS = new printstream (New fileoutputstream (new file ("D:" + file. separator + "test.txt"); string name = "Niuer grazing"; int age = 30; float score = 990.356f; char sex = 'M'; PS. printf ("Name: % s; age: % s; score: % s; Gender: % s", name, age, score, sex); PS. close ();}}
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.