java - 在程序里读取磁盘文件时,为什么需要使用缓冲区。
伊谢尔伦
伊谢尔伦 2017-04-17 17:44:56
0
6
924

java中,读取磁盘文件时,通常都使用BufferedInputStream包装FileInputStream。
但是操作系统内核读取磁盘文件时,不是会将内存作为磁盘文件的缓冲区吗?
那为什么还要在程序里自己使用缓冲区??

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(6)
Peter_Zhu

Because the operating system needs to isolate user space and system space to protect data security. To be precise, when the operating system reads a file, it reads the file into the system space and then copies it to the user space of the application process. That is the program's own buffer that the questioner mentioned. If the user process is allowed to directly access the buffer of the operating system space, wouldn't the operating system be unsafe?

伊谢尔伦

FileInputStream/FileOutputStream Each call to read()/write() will trigger an IO operation.
BufferedInputStream/BufferedOutputSteam Calling read()/write() will not trigger an IO operation every time. It only writes to the internal buffer. IO will only be triggered when the internal buffer is full or flush() is called. operate.

The fewer IO operations, the better the performance.

小葫芦

When doing IO, the JVM will create a byte array as a buffer in its own heap space. Generally, users cannot directly call the memory (directbuffer) through the JVM. The interaction between the heap space buffer and the memory is managed by the JVM. In this way, we can take advantage of JVM advantages, such as GC mechanism and Java high-level encapsulated API.

洪涛

The operating system will not only read the files on the hard disk into the memory for use as cache, but will also open up another space in the memory for caching the data in the memory.
What’s the point? For management and efficiency.
The data exchange rate between memory and memory is two orders of magnitude higher than the data exchange rate between memory and hard disk.
When a process on the memory needs data, if there is matching data in the memory cache, it will be fetched directly from the cache area.
If not, it will be read from the hard disk. (The same goes for writing)

The significance of Java designing cache for IO operations is also here.
The difference is that the data cached by Java can only be used by the current running environment of Java.
Compared with frequently fetching data from the operating system cache, this improves execution efficiency.
This is especially true when your code requires frequent IO operations.

小葫芦

Most of the so-called buffers are encapsulated in the interface area by hard disk hardware, and the buffer area is not large. The operating system does not cache file contents into memory.

巴扎黑

Because your hard drive is not fast enough!

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!