This lesson referrs to buffered I/O while explaining the
flush()
method in the
OutputStream
class.
Buffering is an important feature of Java I/O and is used in most programs that perform I/O.
Buffering involves using a special memory buffer as an intermediary when reading or writing data. Without buffering, when you call the
write()
method data is immediately written to a stream, even if it is just a few bytes.
In this situation, each subsequent call to the
write()
method results in data being written to the stream, and therefore to the underlying physical output device. It would be much more efficient if data was stored away and written to the stream in larger chunks. This efficiency is the premise behind buffering.
A buffered output stream has a memory buffer associated with it which receives data intended for the stream. When the buffer gets full, the entire contents of it are written to the stream and the buffer is cleared for more data.
Buffering results in fewer overall write operations to physical devices, and therefore better performance.
Buffered input works similarly, except that blocks of data are initially read into a buffer and then read operations take place with respect to the buffer .