英文:
How does BufferedStreamReader work in java
问题
The advantage of BufferedReader seems to be the reduction of hard disc accesses. But how does that work?
Sure instead of reading every single byte, StreamBuffer reads a whole chunk of 1000 Bytes for example. But how can he do that?
On the hard disk all the bytes of a file are maybe not continuous, like in an array, but rather dispersed because a file is not written in one flush. But maybe the operating system always in advance reserves 1000 Bytes for a file and if that is consumed it will reserve another 1000 Bytes.
Who is managing what address each byte of a file corresponds on the hard drive?
Can you say to the hard disk drive controller give me 1000 Bytes of a file by just one read command?
英文:
The advantage of BufferedReader seems to be the reduction of hard disc accesses. But how does that work?
Sure instead of reading every single byte, StreamBuffer reads a whole chunk of 1000 Bytes for example. But how can he do that?
On the hard disk all the bytes of a file are maybe not continuous, like in an array, but rather dispersed because a file is not written in one flush. But maybe the operating system always in advance reserves 1000 Bytes for a file and if that is consumed it will reserve another 1000 Bytes.
Who is managing what address each byte of a file corresponds on the hard drive?
Can you say to the hard disk drive controller give me 1000 Bytes of a file by just one read command?
答案1
得分: 2
硬盘被分成扇区,因此您从不只读/写1字节。
扇区的分配以及将它们链接到文件的工作是文件系统的任务。可以是Windows的NTFS,Linux的EXT4或其他任何文件系统。它们还会尝试将一个文件的字节放入连续的扇区中,以便更快地进行读/写操作。如果由于磁盘碎片而无法实现此目标,则磁盘访问变得较慢,此时需要进行碎片整理。
磁盘驱动控制器期望读取多于1字节的数据,并始终读取至少一个扇区,并将所有数据放入缓存中。因此,读取另一个字节只访问缓存,而不是物理磁盘。BufferedReader甚至跳过操作系统和文件系统层,管理Java虚拟机内部的缓存,这样做甚至更快,特别是如果您需要解析读取的数据以将其拆分为文本行。
英文:
Hard disks are divided into sectors, so you never read/write only 1 byte.
The allocation of sectors and linking them into files is the job of the filesystem. It can be NTFS for Windows or EXT4 for Linux or any other. They also try to put bytes of one file into consecutive sectors so they are faster to read/write. If it is not possible because of disk fragmentation, the disk access becomes slower and defragmentation is desired.
The disk drive controller expects to read more than 1 byte and always reads at least one sector and puts all the data in a cache. So reading another byte only accesses the cache, not the physical disk. BufferedReader skips even the operating system and filesystem layers and manages a cache inside Java Virtual Machine which is even faster, especially if you need to parse the read data to split it into text lines.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论