Java->io Stream _ print stream

Source: Internet
Author: User

打印流的概述

打印流The ability to add output data makes it easy to print various data value representations.

The print flow is categorized according to the flow:

Never throw an IO exception

L byte print stream printstream

L character Print stream printwriter

L Method:

void print (String str): Outputs any type of data,

void println(String str): Outputs any type of data, automatically writes a line-break operation

L Code Demo:

/*

* Requirements: Write the specified data to the PrintFile.txt file

*

Analysis

* 1, create a stream

* 2, write data

* 3, close the stream

*/

Public class Printwriterdemo {

Public Static void Main (string[] args) throws IOException {

//Create stream, can pass OutputStreamWriter object also can pass path string

PrintWriter out = new PrintWriter (New FileWriter ("PrintFile.txt"));

PrintWriter out = new printwriter ("PrintFile.txt");

2, write Data

for (int i=0; i<5; i++) {

out.println ("HelloWorld");

}

3, close the stream

Out.close ();

}

}

Print flow complete data refresh automatically

The automatic refresh function of file data can be done by constructing method.

L Construction Method:

L Open File Auto- Refresh write function

Public PrintWriter (OutputStream out, boolean autoflush)

Public PrintWriter (Writer out, boolean AutoFlush)

L Code Demo:

/*

Analysis

* 1, create a stream

* 2, write data

*/

Public class PrintWriterDemo2 {

Public Static void Main (string[] args) throws IOException {

Create a Stream

PrintWriter out= New printwriter(new outputstreamwriternew fileoutputstream("d:\\temp\\ Test.txt ",true)," GBK "),true);

  ↑ Whether to continue writing    ↑ Character Set ↑ Whether to flush writes
Write Data
Out.println ("Tom");
Out.print ("Hello");

3, close the stream

Out.close ();

}

}

Java->io Stream _ 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.