Java Fundamentals Hardening IO Stream Note 59: Print Stream

Source: Internet
Author: User

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

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.