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