简体   繁体   中英

java.io and file system block size

Suppose the file system block size is 4KB, according to my understanding, that means all IO will be at size 4KB. I have 2 questions:

  1. When using java.io API, say FileReader, it doesn't read one char each time, instead, it reads 4KB each time into memory. Is it right?
  2. If the above is true, then it makes no sense to set buffer size smaller than 4KB when using something like BufferedReader, right? Because at minimum we will fetch 4KB into memory every time.

Thank you.

To answer your first question: No. It reads the size of the disk block into kernel memory. Java (and programs written in any language) doesn't have access to that memory. When you do a raw read, there is no guarantee that the block read by the system remains in memory, or that it remains easily reachable in a way that won't be expensive to access subsequently. Here is a possible scenario:

Program: “I need a byte from this file.”
OS: “Loading 4KB block from file. Here is the byte from that block which you requested.”
Program: “Thank you. I have no more need of that block.”
OS: “Okay. Freeing (or setting aside) the 4KB block since no one needs it anymore.”

Program: “My loop needs another byte from this file.”
OS: “Loading 4KB block from file. Here is the byte from that block which you requested.”
Program: “Thank you. I have no more need of that block.”
OS: “Okay. Freeing (or setting aside) the 4KB block since no one needs it anymore.”

etc.

Of course, I have greatly oversimplified things. The OS is likely to cache the block it has read. But you have no way of knowing for how long. Maybe milliseconds, maybe many minutes, maybe not at all.

This isn't just a consideration in Java. C has had separate read(2) and fread(3) functions for a long time.

See also: When and why to use buffered input and output streams?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM