IO stream for Java: print stream

Source: Internet
Author: User

Print Flow:

The print stream is the most convenient class for outputting information, note that contains the byte print stream printstream and the character print stream: PrintWriter. The print flow provides a very convenient printing function,

You can print any type of data information, such as: Decimal, Integer, String.

Review:

Before printing the information need to use outputstream but this way, all the data output will be very troublesome, string-->byte[], the print flow can be easily output,

Common methods in the PrintStream class:

Many of the print () and println () methods are defined in this class, and the System.out.print () method can print any data type.

Construction Method:

 Public PrintStream (OutputStream out)  -- Specify the output location

This construction method receives a subclass of OutputStream,

Print Flow Benefits:

By defining the construction method can be found that there is a construction method can directly receive OutputStream class instances, compared with outputstream, PrintStream can be more convenient output data,

It is equivalent to repackaging the OutputStream class to make the output more convenient.

Example 1: Use PrintStream to output information.

 Packageclass set;ImportJava.io.* ; Public classprintdemo01{ Public Static voidMain (String arg[])throwsexception{PrintStream PS=NULL;//declaring a print stream object//If you are using Fileouputstream instantiation, it means that all output is to the filePS =NewPrintStream (New FileOutputStream(NewFile ("D:" + File.separator + "Test.txt"))) ; PS.Print ( "Hello") ; PS.println ( "World!!!") ; PS.Print ( "1 + 1 =" + 2) ;     ps.close () ; }};       

Execution Result:

In other words, the functionality of the FileOutputStream class is packaged. Such a design is called the decorative design pattern in Java.

Formatted output:

Java has expanded the PrintStream functionality, adding formatted output functionality. You can use print directly. However, the output needs to specify the data type of the output.

This is similar to the C language.

Example 2: The code is as follows:

 Packageclass set;ImportJava.io.* ; Public classprintdemo01{ Public Static voidMain (String arg[])throwsexception{PrintStream PS=NULL;//declaring a print stream object//If you are using Fileouputstream instantiation, it means that all output is to the filePS =NewPrintStream (New FileOutputStream(NewFile ("D:" + File.separator + "Test.txt"))) ; String name= "Li Xinghua";//Defining Strings        intAge = 30;//defining integers        floatScore = 990.356f;//Defining decimals        CharSex = ' M ';//Defining charactersPs.printf ("Name:%s; age:%d; score:%f; Gender:%c" , name,age,score,sex) ;    Ps.close (); }};

Execution Result:

If you feel you want to use a lot of%s,%d,%c cannot remember, the instance can all use "%s" to represent.

ImportJava.io.* ; Public classprintdemo03{ Public Static voidMain (String arg[])throwsexception{PrintStream PS=NULL;//declaring a print stream object//If you are using Fileouputstream instantiation, it means that all output is to the filePS =NewPrintStream (NewFileOutputStream (NewFile ("D:" + File.separator + "Test.txt"))) ; String name= "Li Xinghua";//Defining Strings        intAge = 30;//defining integers        floatScore = 990.356f;//Defining decimals        CharSex = ' M ';//Defining charactersps.printf ("Name:%s; age:%s; score:%s; Gender:%s", Name,age,score,sex);    Ps.close (); }};

IO stream for Java: print stream

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.