Course contentsShow
← Java
Lesson 1951 of 2,87039. File I/O: Basic Byte StreamsFree lesson

Unbuffered vs Buffered I/O: Performance Impact

Compare reading/writing one byte at a time versus using buffers; understand why unbuffered I/O causes excessive system calls.

Unbuffered vs Buffered I/O: Performance Impact

What you'll learn: How buffering dramatically reduces system calls and improves I/O performance compared to unbuffered byte-by-byte operations.

The Core Problem with Unbuffered I/O

When you read or write one byte at a time using FileInputStream or FileOutputStream directly, each read() or write() call triggers a system call to the operating system. System calls are expensive—they involve context switching between your Java application and the OS kernel, which takes time.

Imagine needing to photocopy 1,000 pages, but you walk to the copy room for each single page. That's unbuffered I/O: you're making 1,000 trips when you could carry a stack.

How Buffering Solves This

BufferedInputStream and BufferedOutputStream work by maintaining an internal byte array (buffer, typically 8KB). Instead of making a system call for each byte:

  • Buffered reading: One system call fills the entire buffer with many bytes. Subsequent read() calls pull from memory until the buffer empties, then another system call refills it.
  • Buffered writing: Bytes accumulate in the buffer. Only when it's full (or you call flush() or close()) does a system call write the batch to disk.

The Performance Impact

Reading or writing a 1MB file one byte at a time requires 1,048,576 system calls unbuffered. With an 8KB buffer, you need only about 128 system calls—a reduction of over 99.9%! This translates to drastically faster execution times, often 100x or more in real-world scenarios.

The Takeaway

Key Takeaway: Unbuffered I/O causes excessive system calls because each byte operation communicates directly with the OS; buffered I/O batches operations into memory, reducing system calls by orders of magnitude and dramatically improving performance.