FileInputStream 已经使用缓冲区了吗?

发布于 2024-12-04 20:44:12 字数 295 浏览 1 评论 0原文

当我使用 FileInputStream 读取一个对象(比如几个字节)时,底层操作是否涉及:

1)读取整个磁盘,这样如果我随后执行另一个读取操作,则不需要真正的磁盘读取是因为文件的该部分已在上次读取操作中获取?

或者

2) 发生新的磁盘访问,因为 FileInputStream 不做任何缓冲,而应该使用 bufferedInputStream 来实现 (1) 的效果?

我认为由于 FileInputStream 使用 read 系统调用并且它仅从硬盘读取一组页面,因此必须进行一些缓冲。

When I am using FileInputStream to read an object (say a few bytes), does the underlying operation involve:

1) Reading a whole block of disk so that if I subsequently do another read operation, it wouldnt require a real disk read as that portion of the file was already fetched in the last read operation?

OR

2) A new disk access to take place because FileInputStream does not do any buffering and bufferedInputStream should have been used instead to achieve the effect of (1)?

I think that since the FileInputStream uses the read system call and it reads only a set of pages from hard disk, some buffering must be take place.

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

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

发布评论

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

评论(3

她比我温柔 2024-12-11 20:44:12

FileInputStream 将进行底层本机系统调用。大多数操作系统都会为此进行自己的缓冲。因此它不需要对每个字节进行真正的磁盘寻道。但是,您仍然需要支付本机操作系统调用的成本,这是昂贵的。所以 BufferedStream 会更好。但是,对于读取少量数据(如您所说,几个字节甚至 kB),任何一种都应该没问题,因为操作系统调用的数量不会有太大不同。

FileInputStream will make an underlying native system call. Most OSes will do their own buffering for this. So it does not need a real disk seek for each byte. But still, you have the cost of making the native OS call, which is expensive. So BufferedStream would be preferable. However, for reading small amounts of data (like you say, a few bytes or even kBs), either one should be fine as the number of OS calls won't be that different.

童话 2024-12-11 20:44:12

Native code for FileInputStream is here: it doesn't look like there is any buffering going on in there. The OS buffering may kick in, but there's no explicit indicator one way or another if/when that happens.

森林很绿却致人迷途 2024-12-11 20:44:12

需要注意的一件事是通过慢速连接从已安装的网络卷中读取数据。我为此使用非缓冲 FileInputStream 遇到了很大的性能问题。在开发中没有发现它,因为文件系统是本地的。

One thing to look out for is reading from a mounted network volume over a slow connection. I ran into a big performance issue using a non-buffered FileInputStream for this. Didn't catch it in development, because the file system was local.

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