Java Fundamentals Hardening IO stream Note 60: Print flow improvements case for copying text files

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.