1. Print Flow
(1) Classification:
• Byte print stream PrintStream
• character print stream PrintWriter
(2) Characteristics of the print flow:
• can only write data , cannot read data
• Only the destination can be manipulated and the data source cannot be manipulated
• can manipulate any type of data
• automatically refresh if automatic refresh is enabled
• the ability to manipulate text files directly
(3) Attachment problem: which objects can directly manipulate the text file?
• FileInputStream
• FileOutputStream
• FileReader
• FileWriter
• PrintStream
• PrintWriter
2. PrintWriter of the print stream (character print stream):
(1)PrintWriter Basic use:
1 Packagecn.itcast_03;2 3 Importjava.io.IOException;4 ImportJava.io.PrintWriter;5 6 Public classPrintwriterdemo {7 Public Static voidMain (string[] args)throwsIOException {8 //use as a subclass of writer9PrintWriter PW =NewPrintWriter ("Pw.txt");Ten OnePw.write ("Hello"); APw.write ("World"); -Pw.write ("Java"); - the pw.close (); - } -}
The results are as follows:
(2) PrintWriter operation of any type of data:
The code is as follows:
1 Packagecn.itcast_03;2 3 ImportJava.io.FileWriter;4 Importjava.io.IOException;5 ImportJava.io.PrintWriter;6 7 /*8 * 1: Any type of data can be manipulated. 9 * PRINT ()Ten * println () One * 2: Start auto Refresh A * PrintWriter pw = new PrintWriter (New FileWriter ("Pw2.txt"), true); - * or you should call the println () method to - * This time not only automatically refreshed , but also to achieve the data line-breaking . the * - * println () - * actually equivalent to : - * bw.write (); + * bw.newline (); - * Bw.flush (); + */ A Public classPrintWriterDemo2 { at Public Static voidMain (string[] args)throwsIOException { - //Create a Print flow object - //PrintWriter pw = new PrintWriter ("Pw2.txt"); -PrintWriter PW =NewPrintWriter (NewFileWriter ("Pw2.txt"),true); - - //write () is uncertain, how to do ? in //we should see a new way of it. - //Pw.print (true); to //Pw.print (+); + //pw.print ("Hello"); - thePw.println ("Hello"); *Pw.println (true); $PW.PRINTLN (100);Panax Notoginseng - pw.close (); the } +}
Run the effect as follows:
Java Fundamentals Hardening IO Stream Note 59: Print stream