[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 ();}}