PrintWriter Print Flow instance

Source: Internet
Author: User
Tags print object

A good memory is better than a pen.

Attention:
PrintWriter only output write function, but no read function
PrintWriter is the character output, PrintStream is the character output
The bottom of the PrintWriter is still implemented by BufferedWriter and Bufferedstreamwriter.

Package com.

Ckinghan.iodemo;
Import Java.io.BufferedReader;
Import java.io.FileNotFoundException;
Import Java.io.FileReader;
Import Java.io.FileWriter;
Import java.io.IOException;

Import Java.io.PrintWriter;
        public class Printstreamdemo {/** * @ Description: Print Stream * creation time:/public static void main (string[] args) {

        /** * Use print stream to write to File/PrintWriter ();

        /** * Use automatic refresh print stream to write to file */Printwriterautoflsh ();
    /** * Read the contents of the file, use PrintWriter to print to the console/Readwritercontroller ();
        /** * @ Description: Read the contents of the file, use PrintWriter to print to the console * creation time: * * public static void Readwritercontroller () {
        BufferedReader bufferedreader = null;
        PrintWriter printwriter = null;
            try {BufferedReader = new BufferedReader (New FileReader ("Printstreamauto.java")); /** * Instantiate PrintWriter Print object, and turn on automatic refresh function * * PrintWriter = new PrinTwriter (system.out,true);
            String readerstring = null;
                 while ((readerstring = Bufferedreader.readline ())!= null) {/** * outputs data to the console   * * Because the automatic refresh function is turned on, println is equivalent to the following method * Printwriter.write (string string);                  Write character * Printwriter.newline ();                             linefeed * PRINTWRITER.FLSH ();
            Empty cache/Printwriter.println (readerstring);
        } catch (FileNotFoundException e) {e.printstacktrace ();
        catch (IOException e) {e.printstacktrace (); }/** * @ Description: Write to file with automatic refresh (* * Creation time: * * public static void Printwriterautoflsh () {/
        /Create a print stream object printwriter printwriter = null;
    try {/** * Note that printwriter (new Writer,boolean Autoflsh) can set whether the buffer data can be automatically refreshed         * If each parameter is a string type, it is not a Boolean autoflsh parameter, note/printwriter = new PrintWriter (New Fil
            Ewriter ("Printstreamauto.java"), true); /** * Writes data to a file * * Because the automatic refresh feature is turned on, the println corresponds to the following method of executing * printwrit   Er.write (string string);                  Write character * Printwriter.newline ();                             linefeed * PRINTWRITER.FLSH ();
            Empty cache/Printwriter.println ("I turned on the PrintWriter automatic buffering refresh function");

        Printwriter.println ("I'm going to be written to the file, waiting to be read");
        catch (FileNotFoundException e) {e.printstacktrace ();
        }catch (IOException e) {e.printstacktrace ();
            }finally {//close stream if (PrintWriter!= null) {printwriter.close (); /** * @ Description: Write to file with print stream * creation time:/public static void PrintWriter () {//Create
  Print Stream objects      PrintWriter printwriter = null;
            try {//materialized print Stream object printwriter = new PrintWriter ("Printstream.java");
            Write data to a file Printwriter.println ("In the middle of a violation of plastic technology laws 塉 waste plastics, whether the law feel");
        Empty buffer data printwriter.flush ();
        catch (FileNotFoundException e) {e.printstacktrace ();
            }finally {//close stream if (PrintWriter!= null) {printwriter.close (); ---------------------------------------------------------------------execution Results---------------------------}}}}
 ------------------------------------------I turned on the PrintWriter automatic buffering refresh feature I'm going to be written to the file, waiting to be read

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.