Differences in the use of PrintWriter and BufferedWriter

Source: Internet
Author: User
Tags readline

bufferedwriter: Writes text to the character output stream, buffering individual characters to provide efficient writes of individual characters, arrays, and strings. The Write () method allows you to output the acquired character and then wrap it through newline (). The character stream in BufferedWriter must be brushed out by calling the Flush method. and BufferedWriter can only operate on character streams. If you want to operate on a byte stream, use Bufferedinputstream.


printwriter: Prints the formatted representation of the object to the text output stream (prints formatted representations of objects to a text-output stream). The advantage of PrintWriter relative to BufferedWriter is that if PrintWriter is enabled for automatic refresh, then when PrintWriter invokes the PRINTLN,PRINLF or Format method, The data in the output stream is automatically refreshed. PrintWriter can not only receive character streams, but also can receive byte streams.


socket programming, as far as possible with PrintWriter to replace BufferedWriter, the following is the advantages of PrintWriter:

1. PrintWriter's print and println methods can accept parameters of any type, while the write method of BufferedWriter can only accept characters, array of characters, and strings;

2. PrintWriter's Println method automatically adds line wrapping, BufferedWriter needs to display the calling newline method;

3. The PrintWriter method does not throw the exception, if concerns the exception, needs to call the CheckError method to see whether has the unusual occurrence;

4. PrintWriter construction method can specify parameters, realize automatic refresh cache (AutoFlush);

5. The PrintWriter method is more widely constructed.

-------------------------------------------------------------------

Summary:

When you use the ReadLine method in BufferedReader to receive a character stream in BufferedWriter, the entire line of characters is returned because ReadLine is reading to a newline character. So the BufferedWriter method after entering a paragraph of characters to use the newline method to do a newline operation, and then the character stream brush out. and PrintWriter because the automatic refresh can be turned on, and the Println method in which it takes the line-wrapping operation. So code is easier to implement than BufferedWriter.

PrintWriter and BufferedWriter are inherited java.io.Writer, so many functions are the same. However, PrintWriter provides a println () method to write line breaks for different platforms, and BufferedWriter can set any buffer size. OutputStream can be passed directly to PrintWriter (BufferedWriter cannot receive), such as:

PrintWriter out = new PrintWriter (new Bufferedoutputstream) (New
FileOutputStream ("Foo.out"));
or use OutputStreamWriter to convert OutputStream to Wrtier. Then you can use the BufferedWriter. The JDK API is clearly written in documents.

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.