Java. IO input/output stream: Filter stream: Buffer stream and data stream

Source: Internet
Author: User

java.io uses design patterns such as adapter mode decoration mode to solve the problem of socket and input and output of character streams.

The byte stream can only process one bytes at a time, and in order to manipulate data more conveniently, it joins the socket stream.

Problem Introduction: Why is buffered stream more efficient than normal file-byte throttling?

Without buffering, a byte is written for each byte read.

Because disk-related IO operations are much slower than memory operations, streaming without buffering is inefficient.

With buffered streams, you can read many bytes at a time, but not write to the disk, just put it in memory first.

When the buffer size is enough to write to the disk once, this way can reduce the number of disk operations, speed will improve a lot!

This is the difference between the two.

Approximate process:

There is no buffer, then every read once, will send an IO operation;

There is a buffer, the first time read, it reads x bytes into the buffer, then the subsequent read will be read from the cache, when read to the end of the buffer, will read the X bytes into the buffer.

Obviously, the second way, it will reduce the IO operation, more efficient, the disadvantage is that memory consumption of more.

Java input and output streams are designed in a multilayer package

The bottom inputstream and OutputStream are based on byte stream, there is no caching mechanism, generally used with bufferinputstream and Bufferoutputstream encapsulation.

The Read method of the Bufferinputstream is to block the thread, and Bufferinputstream.read (BUF) returns all of the input stream after it is read into the BUF.

Bufferoutputstream.write (BUF) will output the contents of the BUF to the output stream, but remember to flush;

There is a more useful printstream and PrintWriter similar can be automatically refreshed just for the word stream.

Byte streams are typically used to transfer binary files, and so on, the character stream is often used with reader for wrapping.

The most commonly used have Bufferinputstreamreader and Printwrinter, Bufferinputstreamreader ReadLine method is practical to encounter \r\d will automatically flush.

Printwrinter as long as the Refresh property is set to true in the constructor, its Println method can be automatically refreshed without flush.

FilterInputStream and Filteroutputstream: filtered streams, buffer streams, and data streams inherit from this.

For a buffer stream, the data is actually sent to the output stream only when the buffers are full, but you can use the flush () method to manually send data from a buffer that has not yet been filled, and cannot determine how the file is encoded, which is difficult to apply on the network.

The most practical use is: The data stream allows the sender and receiver to follow the same encoding to handle.

  DataInputStream and DataOutputStream: can accept a row of data, you can encode it, or it can be a socket stream, you can socket file byte stream and network byte stream, Read and write in the same order, otherwise the reading will appear an exception.

DataInputStream is used to decorate other input streams, which "Allow applications to read basic Java data types from the underlying input stream in a machine-independent manner." applications can use DataOutputStream (data output stream) to write data that is read by the DataInputStream (data input stream).

Java. IO input/output stream: Filter stream: Buffer stream and data 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.