lush() java 文件处理

发布于 2024-09-29 16:02:28 字数 45 浏览 4 评论 0原文

lush() 的具体用途是什么?流和缓冲区有什么区别?为什么我们需要缓冲区?

What is the exact use of flush()? What is the difference between stream and buffer? Why do we need buffer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

念三年u 2024-10-06 16:02:28

缓冲的优点是效率。通常,将 4096 字节的块写入文件一次比写入 4096 次一个字节要快。

缓冲的缺点是您会错过反馈。句柄的输出可以保留在内存中,直到写入足够的字节以使其值得写入文件句柄。程序的一部分可能会将一些数据写入文件,但程序的不同部分或不同的程序无法访问该数据,直到程序的第一部分将数据从内存复制到磁盘。根据数据写入该文件的速度,这可能需要任意长的时间。

当您调用flush()时,您要求操作系统立即将缓冲区中的所有数据写入文件句柄,即使缓冲区未满。

The advantage of buffering is efficiency. It is generally faster to write a block of 4096 bytes one time to a file than to write, say, one byte 4096 times.

The disadvantage of buffering is that you miss out on the feedback. Output to a handle can remain in memory until enough bytes are written to make it worthwhile to write to the file handle. One part of your program may write some data to a file, but a different part of the program or a different program can't access that data until the first part of your program copies the data from memory to disk. Depending on how quickly data is being written to that file, this can take an arbitrarily long time.

When you call flush(), you are asking the OS to immediately write out whatever data is in the buffer to the file handle, even if the buffer is not full.

吾性傲以野 2024-10-06 16:02:28

数据有时会在实际写入磁盘(在缓冲区中)之前被缓存,刷新会导致缓冲区中的内容写入磁盘。

The data sometimes gets cached before it's actually written to disk (in a buffer) flush causes what's in the buffer to be written to disk.

蓦然回首 2024-10-06 16:02:28

flush 告诉输出流将所有数据发送到底层流。由于内部缓冲,这是必要的。缓冲区的基本目的是最大限度地减少对底层流 API 的调用。如果我将长字节数组存储到 FileOutputStream,我不希望 Java 每个字节调用一次操作系统文件 API。因此,缓冲区在 Java 内部和外部的各个阶段使用。即使您确实每次调用 fputc 一次字节,操作系统不会每次都真正写入磁盘,因为它有自己的缓冲。

flush tells an output stream to send all the data to the underlying stream. It's necessary because of internal buffering. The essential purpose of a buffer is to minimize calls to the underlying stream's APIs. If I'm storing a long byte array to a FileOutputStream, I don't want Java to call the operating system file API once per byte. Thus, buffers are used at various stages, both inside and outside Java. Even if you did call fputc once per byte, the OS wouldn't really write to disk each time, because it has its own buffering.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文