Print streams for Java 21-12 IO streams

Source: Internet
Author: User

Print Flow
byte stream Print stream PrintStream
Character Print stream PrintWriter

Characteristics of the print stream:
A: Only the data is written, no data is read. Only the destination can be manipulated and the data source cannot be manipulated. (data can only be written to the file, not from the file)
B: You can manipulate any type of data.
C: Automatically refreshes if automatic refresh is initiated.
D: The stream is able to manipulate the text file directly.

Which stream objects can manipulate text files directly?
FileInputStream
FileOutputStream
FileReader
FileWriter
PrintStream
PrintWriter
Look at the API, the construction method of the check stream object, if there are both file type and string type parameters, generally speaking, you can manipulate the file directly.

Flow if divided by operation:
Basic flow: Is the ability to read and write files directly
Advanced Streaming: Provides some additional functionality on a basic stream basis

Use as a subclass of writer:

1      Public Static voidMain (string[] args)throwsIOException {2         //use as a subclass of writer3PrintWriter PW =NewPrintWriter ("Pw.txt");4 5Pw.write ("Hello");6Pw.write ("World");7Pw.write ("Java");8         9 pw.close ();Ten}

Characteristics of the print stream:

1: You can manipulate any type of data.
Print ()
println ()
2: Start Auto Refresh
PrintWriter pw = new PrintWriter (New FileWriter ("Pw2.txt"), true);
Or should I call the println () method to
This time not only automatically refreshed, but also to achieve the data line-breaking.

Here's println ()
In fact, it is equivalent to:
Bw.write ();
Bw.newline ();
Bw.flush ();

PrintWriter for automatic refresh and line wrapping:

1  Public Static voidMain (string[] args)throwsIOException {2         //Create a Print flow object3PrintWriter PW =NewPrintWriter (NewFileWriter ("Pw2.txt"),true);4 5Pw.println ("Hello");6Pw.println (true);7PW.PRINTLN (100);8 9 pw.close ();Ten}

Print streams for Java 21-12 IO streams

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.