1. Use the print stream to improve the case for copying a text file
2. code example:
1 Packagecn.itcast_03;2 3 ImportJava.io.BufferedReader;4 ImportJava.io.BufferedWriter;5 ImportJava.io.FileReader;6 ImportJava.io.FileWriter;7 Importjava.io.IOException;8 ImportJava.io.PrintWriter;9 Ten /* One * Requirements: Datastreamdemo.java copied to Copy.java A * Data Source: - * Datastreamdemo.java--read data--filereader--BufferedReader - * Destination: the * Copy.java--Write data--FileWriter--bufferedwriter--PrintWriter - */ - Public classCopyfiledemo { - Public Static voidMain (string[] args)throwsIOException { + //Previous Versions - //Encapsulating Data Sources + //bufferedreader br = new BufferedReader (New FileReader ( A //"Datastreamdemo.java")); at // //Package Destination - //bufferedwriter bw = new BufferedWriter (New FileWriter ("Copy.java")); - // - //String line = null; - //While (line = Br.readline ()) = null) { - //Bw.write (line); in //bw.newline (); - //Bw.flush (); to // } + // - //bw.close (); the //br.close (); * $ //improved version of the print streamPanax Notoginseng //Encapsulating Data Sources -BufferedReader br =NewBufferedReader (NewFileReader ( the"Datastreamdemo.java")); + //Package Destination APrintWriter PW =NewPrintWriter (NewFileWriter ("Copy.java"),true); the +String line =NULL; - while((Line=br.readline ())! =NULL){ $ pw.println (line); $ } - - pw.close (); the br.close (); - }Wuyi}
Java Fundamentals Hardening IO stream Note 60: Print flow improvements case for copying text files