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

Disk I/O Latency and Sequential vs Random Access

Learn why sequential file access is faster than random access and how byte streams interact with disk performance.

Disk I/O Latency and Sequential vs Random Access

You'll learn why sequential file reading is dramatically faster than jumping around and how this impacts your byte stream operations.

Understanding Disk I/O Latency

When your Java program reads or writes bytes using FileInputStream or FileOutputStream, it ultimately interacts with physical storage. This hardware has latency — the time delay between requesting data and actually receiving it.

Think of reading a book: sequential reading means you turn pages one after another (fast), while random access is like constantly flipping to different chapters (slow, requiring time to find each page).

Sequential vs Random Access

Sequential access means reading or writing bytes in consecutive order from start to finish. Modern disks (even SSDs) are optimized for this pattern. When you read sequentially:

  • The disk's read head stays in one area (HDDs) or the controller can predict the next block (SSDs)
  • Operating system prefetching kicks in, loading data ahead of your requests
  • Fewer seek operations = less latency overhead

Random access means jumping to different file positions frequently. Each jump requires:

  • A seek operation (positioning the read head on HDDs)
  • Cache misses and prediction failures
  • Breaking the natural flow of data retrieval

Impact on Byte Streams

When you use read(byte[]) to process a file from beginning to end, you're doing sequential I/O — fast and efficient. But if your code frequently repositions within the file or reads scattered locations, you're doing random access, which multiplies latency costs.

This is why buffered streams help so much: they read ahead sequentially in large chunks, even if your code requests data byte-by-byte.

Key Takeaway: Sequential file access minimizes disk latency by reading consecutive bytes, while random access patterns incur repeated positioning overhead. Always process files sequentially with byte streams when possible.