2

From this tutorial,

Most of the examples we've seen so far use unbuffered I/O. This means each read or write request is handled directly by the underlying OS. This can make a program much less efficient, since each such request often triggers disk access, network activity, or some other operation that is relatively expensive.

To reduce this kind of overhead, the Java platform implements buffered I/O streams. Buffered input streams read data from a memory area known as a buffer; the native input API is called only when the buffer is empty. Similarly, buffered output streams write data to a buffer, and the native output API is called only when the buffer is full.

I understand that operations like disk-access, network activity etc. would cause memory or execution-time overhead for the underlying OS.

But the question is that how does the program reading-from/writing-to a certain memory area (the buffer) reduce this overhead?

I see this as an addition of a couple extra steps: First the program requests the OS to, e.g., read data from a file and write it to the buffer, and then the program reads it from the buffer.

Solace
  • 8,612
  • 22
  • 95
  • 183
  • 2
    Well, my RAM is much faster than my HDD and has a better connection to the CPU than both HDD or SSD. – Tom Feb 22 '16 at 17:45
  • 1
    In addition to @Code-Apprententice 's answer you could check out this http://stackoverflow.com/questions/9648811/specific-difference-between-bufferedreader-and-filereader – Chad Dienhart Feb 22 '16 at 17:49

1 Answers1

2

As you probably know, IO operations with a disk drive, network connection, or another device are much slower than memory access. By buffering IO operations in memory, software can reduce the number of operations performed on the IO device.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Thank you. But does this also mean that the last paragraph from my question is correct, i.e. using the buffered streams does still involve requesting the OS to fetch data from, say disk drive, and to store it in our buffer; from where our program is going to read it. I mean should I take away from this that buffers are useful because once the data has been put there in the buffer (by the underlying OS), our program can read it again and again, a number of times, without requesting the OS each time. – Solace Feb 22 '16 at 18:24
  • 1
    @Solace Yes, by buffering we can reduce the number of requests to the OS to perform an IO operation. This is not only useful for reading the same data again and again but also for reading contiguous data from, say, a file or network. – Code-Apprentice Feb 22 '16 at 18:35