Java IO learning notes (4) print stream and io learning notes
1. Only the output stream has a print stream: PrintWriter and PrintStream provide a heavy print for characters and bytes respectively. The Println method is used for output of multiple data types. The PrintWriter and PrintStream operations do not throw an exception. If the data is not printed, the system does not throw an exception. 2. System. out. print (Object object) calls the toString () method of the Object class. 3. PrintWriter and PrintStream have the automatic flush function and do not need to manually call the flush method. * It must be emphasized that the automatic flush function of PrintWriter is automatically flush only when PrintWriter (Writer out, boolean autoFlush) is called and autoFlush is set to true, otherwise, it will not be automatically flush and must be manually printWriter. flush () can print data successfully. In addition, the PrintWriter method can not only be set outside the Writer output stream but also outside the byte output stream. The PrintWriter construction method is as follows: 4. System. out. in println (), java. lang. system. the out is of the PrintStream type. Exercise applet 1:PackageTest. io. print;ImportJava. io. FileNotFoundException;ImportJava. io. FileOutputStream;ImportJava. io. IOException;ImportJava. io. PrintStream;Public ClassPrintStreamTest {Public Static VoidMain (String args []) {PrintStream ps =Null; FileOutputStream fos =Null;Try{Fos =NewFileOutputStream ("E:/technology learning/java/test/Socket/test6.txt"); ps =NewPrintStream (fos); System.SetOut(Ps); // output to the file pointed to by FileOutputStream instead of console output.For(IntI = 0; I <3000; I ++) {System.Out. Print (I + "");}}Catch(FileNotFoundException e) {e. printStackTrace ();}Finally{Try{If(Ps! =Null) {Ps. close ();}If(Fos! =Null) {Fos. close ();}}Catch(IOException e) {e. printStackTrace () ;}}} exercise applet 2: Enter the content from the console and print the content entered in the console to a file, similar to recording the log package test. io. print; import java. io. bufferedReader; import java. io. fileWriter; import java. io. IOException; import java. io. inputStreamReader; import java. io. printWriter; import java. util. date; public class PrintWriterTest {public static void main (String args []) {BufferedReader bf = new BufferedReader (new InputStreamRead Er (System. in); String s = null; PrintWriter pw = null; try {// PrintWriter constructor sets whether automatic flush is set to true to automatically flush. If true is not set, you must manually call the flush method to print the content to the file. pw = new PrintWriter (new FileWriter ("E:/technical learning/java/test/Socket/test7.txt ", true), true); while (s = bf. readLine ())! = Null) {if (s. equals ("exit") {break;} System. out. println (s); pw. println ("-------------------------------------"); pw. println (s); // pw. flush ();} pw. println ("-------------" + new Date () + "------------");} catch (IOException e) {e. printStackTrace ();} finally {try {if (bf! = Null) {bf. close () ;}if (pw! = Null) {pw. close () ;}} catch (IOException e) {e. printStackTrace ();}}}}