Java Print Stream (PrintStream)

Source: Internet
Author: User
Tags string format

Print Flow ( PrintStream): The print stream can print any type of data, and the print stream will convert the data to a string before printing

PrintStream can accept files and other byte output streams, so the print stream is an enhancement to the normal byte output stream, which defines a lot of overloaded print () and println () to facilitate the output of various types of data.

PrintWriter

PrintStream: is a byte print stream, and the system.out corresponding type is printstream.

Its constructor can receive values of three data types.

1, string path.

The 2,file object.

3,outputstream.

Note : Three ways to print a stream

void print (data type variable)

println (data type variable)

printf (String format, Object ... args)

Data formats can be customized

The difference between the print and println methods is that a line break is not wrapped

The Print method and the Write method are not the same, print provides automatic refresh.

the normal write method needs to call the flush or Close method to see the data.

After JDK1.5, Java extended the PrintStream, adding formatted output, and using the printf () overloaded method to format the output directly. However, when formatting the output, you need to specify the format of the output data type.

PrintWriter: is a character print stream.

The constructor can receive four types of values.

1, string path.

The 2,file object.

You can also specify an encoding table for data of type. That is, the character set.

3,outputstream

4,writer

for data of type 3,4, you can specify automatic refresh.

Note: When this auto-refresh value is True, only three methods can be used: Println,printf,format.

If you want to have both an automatic refresh, you can perform the encoding. How do I wrap a stream object?

New PrintWriter ( new Outputsteamwriter (new FileOutputStream ("A.txt"), "Utf-8"),true);

If you want to improve efficiency. You also use the Print method.

New PrintWriter (new Bufferdwriter (new  Outputsteamwriter) (New FileOutputStream ("A.txt") , "Utf-8")),true);

Java Print Stream (PrintStream)

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.