Java 20-9 byte output stream with buffer and byte input stream

Source: Internet
Author: User

By the two ways in which the previous bytes were entered, we can see that the array is read in a way that defines an array much faster than a byte read.

As a result, Java specifically provides a byte class with a buffer:

Buffer Class (Efficient Class)

Write Data : Bufferedoutputstream

Construction Method:

      BufferedInputStream(InputStream in) 创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。

      BufferedInputStream(InputStream in, int size)Creates a specified buffer size BufferedInputStream and saves its arguments, that is, the input stream for in future use.
Read Data : Bufferedinputstream

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) 创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的底层输出流。

Doubts:

Example: Bufferedoutputstream (OutputStream out)
Why not pass a specific file or file path, but pass a OutputStream object?
Reason:

Byte buffer streams provide only buffers and are designed for efficiency. However, the actual read and write operation is also dependent on the basic flow object implementation.
Example: Bufferedoutputstream: Equivalent to a water cup
OutputStream: Equivalent to water, quench your thirst and rely on water

Create an efficient byte output stream object and enter the data

New Bufferedoutputstream (                new fileoutputstream ("C.txt"));

Output data
Out.write ("Javaio". GetBytes ());

Out.close ();

Create a university byte input stream object and output the data

New Bufferedinputstream (new  fileinputstream (                "Bos.txt"));

byte[] bys = new byte[1024];
int len = 0;
while (len = Bis.read (bys))! =-1) {
System.out.print (New String (bys, 0, Len));
}

Freeing resources
Bis.close ();

Java 20-9 byte output stream with buffer and byte input 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.