java.io uses design patterns such as adapter mode decorative mode to solve the socket and input and output problems of character streams.
The byte stream can only process one byte at a time, and in order to make it easier to operate data, the socket stream is added.
Problem introduction:
Why is buffered stream more efficient than ordinary file byte streams?
For operations without buffering, one byte must be written for each byte read.
Since IO operations involving disks are much slower than memory operations, streams without buffering are very inefficient.
A stream with buffer can read many bytes at a time, but does not write to disk, but is just placed in memory first.
When you have enough buffer size, write it to the disk at one time. This method can reduce the number of disk operations and the speed will be much higher!
This is the difference between the two.
Probably process:
If there is no cache area, then every time you read, an IO operation will be sent;
There is a cache area. When you read the first time, x bytes will be read and put into the cache area. Then subsequent reads will be read from the cache. When you read to the end of the cache area, x bytes will be read again and put into the cache area.
It is obvious that the second method will reduce IO operations and be more efficient. The disadvantage is that it consumes more memory.
Java input and output streams are designed using multi-layer encapsulation
The lowest level InputStream and OutputStream are based on byte streams and have no cache mechanism. They are generally encapsulated and used after use by BufferInputStream and BufferOutputStream.
The read method of BufferInputStream blocks the thread. BufferInputStream.read(buf) will read all the input stream into buf before returning.
BufferOutputStream.write(buf); will output the contents in the buf to the output stream, but remember to flush;
There is also a relatively easy-to-use PrintStream and PrintWriter that can be automatically refreshed, but for byte streams.
Byte streams are generally used to transfer binary files and other character streams. They are often wrapped with reader.
The most commonly used ones are BufferInputStreamReader and PrintWrinter. The readline method of BufferInputStreamReader is very practical and will automatically flush when encountering /r/d.
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: Filter flow, buffer flow and data flow are both inherited from here.
For buffer streams, the data will be truly transferred to the output stream only when the buffer is full, but the flush() method can be used to send data from the unfilled buffer artificially; the encoding method of the file cannot be determined and it is difficult to apply on the network.
The most commonly used in practice is that the data stream can allow the sender and the receiver to process according to the same encoding.
DataInputStream and DataOutputStream: Can accept a line of data, can encode it, or it can be a socket stream, can socket file byte stream and network byte stream, and the order of read and write must be consistent, otherwise an exception will occur when reading.
DataInputStream is used to decorate other input streams, which "allows applications to read basic Java data types from the underlying input stream in a machine-independent manner." Applications can use DataOutputStream to write data read by DataInputStream.
The above is the Java.IO input and output stream filtering stream buffer stream and data stream introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!