Java Fundamentals Hardening IO Stream Note 28:bufferedoutputstream/bufferedinputstream (byte buffer stream) Bufferedoutputstream write data

Source: Internet
Author: User

1. Overview of Bufferedoutputstream/bufferedinputstream (byte buffer stream)

It is really much faster to define an array than it was to read one byte at a time, so it seems that a buffer is still very good. Since this is the case, then, when Java began to design, it also took into account this problem, it is specifically provided with a buffer byte class. This class is called: Buffer Class (High efficiency Class)

  Write Data : Bufferedoutputstream
  Read Data : Bufferedinputstream

The construction method can specify the size of the buffer, but we generally do not use it because the default buffer size is sufficient .

2. Bufferedoutputstream Construction method :

BufferedOutputStream(OutputStream out)
creates a new buffered output stream to write data to the specified underlying output stream.
BufferedOutputStream(OutputStream out, int size)
creates a new buffered output stream that writes data with the specified buffer size to the specified underlying output stream.

Bufferedoutputstream construction method, Why not pass a specific file or file path, but pass a OutputStream object?

A: The reason is simple, the byte buffer stream only provides buffers, designed for efficiency. However, the actual read and write operation is also dependent on the basic flow object implementation.

3. Bufferedoutputstream Write Data , code example:

1  Packagecn.itcast_05;2 3 ImportJava.io.BufferedOutputStream;4 ImportJava.io.FileOutputStream;5 Importjava.io.IOException;6 7  Public classBufferedoutputstreamdemo {8      Public Static voidMain (string[] args)throwsIOException {9         //Bufferedoutputstream (OutputStream out)Ten         //FileOutputStream fos = new FileOutputStream ("Bos.txt"); One         //bufferedoutputstream bos = new Bufferedoutputstream (FOS); A         //Simple notation -Bufferedoutputstream BOS =NewBufferedoutputstream ( -                 NewFileOutputStream ("Bos.txt")); the  -         //Write Data -Bos.write ("Hello". GetBytes ()); -  +         //Freeing Resources - bos.close (); +     } A}

Java Fundamentals Hardening IO Stream Note 28:bufferedoutputstream/bufferedinputstream (byte buffer stream) Bufferedoutputstream write data

Related Article

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.