Home  >  Q&A  >  body text

java - 在程序里读取磁盘文件时,为什么需要使用缓冲区。

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

伊谢尔伦伊谢尔伦2765 days ago973

reply all(6)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 17:46:56

    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?

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 17:46:56

    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.

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 17:46:56

    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.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 17:46:56

    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.

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 17:46:56

    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.

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 17:46:56

    Because your hard drive is not fast enough!

    reply
    0
  • Cancelreply