Java I / O Classes and Performance

Reading Thinking in Java 4th ed. I have some doubts about the performance of I / O operations: I read that it is better to "wrap" InputStream objects in a BufferedInputStream, but, in my opinion, I see no difference. Are, for example, file operations already buffered? What are the benefits of file buffering?

+3
source share
3 answers

IO system buffering is at a different level than Buffered * putStream.

Each call FileOutputStream.write(...)invokes a native method call (which is usually more expensive than an internal java call), and then switches the context to the OS kernel for the actual record. Even if the kernel (or the file system driver or the hard disk or the hard disk itself) performs more buffering, these costs will occur.

By wrapping the BufferedOutputStream around this, we will call our own recording method much less frequently, thereby providing much higher throughput.

(The same applies to other types of I / O, of course, I just used FileOutputStream as an example.)

+6
source

Are, for example, file operations already buffered?

, , - , HD, (, ) .. BufferedInputStream, , " ". ( /).

+5

An InputStream , , , 1000 , 1000 , .

A BufferedInputStream, , InputStream , .

The same goes for output, instead of writing each character separately, fewer physical disk entries c BufferedOutputStream.

+2
source

All Articles