在我的记录器代码中,我使用 FileInputStream用于读取日志文件。存储上次从日志文件“byteOffset”中读取的文件字节数。每当读者必须阅读日志时,它会执行 FileInputStream.skip(byteOffset)
并从那里读取。在这种情况下,我从 InputStream.skip()
中找到了以下声明文档。
Skips over and discards n bytes of data from this input stream. The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. This may result from any of a number of conditions; reaching end of file before n bytes have been skipped is only one possibility.
除了文档中提到的内容之外,还有哪些其他原因会导致实际跳过的数据与输入不同?我只想确定在我的日志阅读器代码中我需要为所有情况做好准备。
最佳答案
这取决于实现。也许它是一个缓冲流,它只从后备流中读取了 100 个字节,并且检索更多的字节将花费更多的时间。
(我不认为将那部分放在 InputStream
规范中是一个好的决定,但现在不可能改变。)
关于java - InputStream.skip() 何时跳过少于预期的字节数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12630606/