BufferedReader 中的标记和重置是什么?
我想知道BufferedReader
的mark()
和reset()
方法是什么?我该如何使用它们?我阅读了 Javadoc,但作为初学者我无法理解它。
I would like to know what are the mark()
and reset()
methods of BufferedReader
? How do I use them? I read the Javadoc but as a beginner I was unable to understand it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
流的
mark
和reset
方法提供了一种在流中向后跳转并重新读取数据的方法。当您在
BufferedReader
上调用mark()
时,它将开始将您从该点向前读取的数据保留在其内部缓冲区中。当您调用reset()
时,它将跳回流的标记位置,因此内存缓冲区将满足下一个read()
的要求。当您读取超过该缓冲区末尾时,它将无缝返回读取新数据。BufferedInputStream
的工作方式相同。mark
的 int 参数告诉它您希望能够读取的最大字符数(对于 BufferedReader)或字节数(对于BufferedInputStream
)倒退。如果您读取了超过标记位置的太多数据,则标记可能会“无效”,并且调用reset()
将失败并出现异常。一个小例子:
在该示例中,我将
StringReader
包装在BufferedReader
中以获取readLine()
方法,但是StringReader< /code> 已经支持自己的
标记
和重置
!从内存中数据源读取的流通常支持mark
和reset
本身,因为它们已经在内存中拥有了所有数据,所以它是他们很容易再次阅读它。从文件、管道或网络套接字读取的流自然不支持mark
和reset
,但您始终可以通过将其包装在中来将该功能添加到任何流中BufferedInputStream 或 BufferedReader。
The
mark
andreset
methods of streams provide a way to jump backwards in the stream and re-read data.When you call
mark()
on aBufferedReader
it will begin keeping data you read from that point forwards in its internal buffer. When you callreset()
it will jump back to the marked position of the stream, so the nextread()
s will be satisfied by the in-memory buffer. When you read past the end of that buffer, then it will seamlessly go back to reading fresh data.BufferedInputStream
works the same way.The int parameter to
mark
tells it the maximum number of characters (forBufferedReader
) or bytes (forBufferedInputStream
) that you want to be able to go backwards. If you read too much data past the marked position, then the mark can be "invalidated", and callingreset()
will fail with an exception.A little example:
In that example, I wrapped the
StringReader
in aBufferedReader
to get thereadLine()
method, butStringReader
s already supportmark
andreset
on their own! Streams that read from an in-memory data source usually supportmark
andreset
themselves, because they already have all the data in memory so it is easy for them to read it again. Streams that read from files or pipes or network sockets do not naturally supportmark
andreset
, but you can always add that feature to any stream by wrapping it in aBufferedInputStream
orBufferedReader
.mark()
标记流中的特定点,reset()
将流重置为最新标记。这些方法提供了书签
功能,允许您在流中提前阅读以检查即将到来的数据。从这个文档:
The
mark()
marking a particular point in a stream andreset()
resets the stream to the most recent mark. These methods provide abook-marking
feature that allows you to read ahead in the stream to inspect the upcoming data.From this documentation:
Reader::mark(int readLimit)
文档说:
示例:
输出:
Reader::mark(int readLimit)
documentation say:Example:
Output:
阅读器界面不让您返回,您只能阅读。另一方面,BufferedReader 创建一个缓冲区,因此您可以在读取时返回一点。这就是这些方法的用途。
使用 mark() 方法,您可以将“标记”放置到某个位置,然后您可以继续阅读。一旦你意识到你想要返回标记的位置,你可以使用reset()来实现。从那时起,您再次读取相同的值。您可以将其用于任何您想要的用途。
Reader interface does not let you return, you can just read. BufferedReader, on the other hand, creates a buffer, so you are able to return a bit when reading. And this is what those methods are for.
With mark() method you put a "marker" to a position, then you can read on. Once you realize you want to return the the marked position you use reset() for that. And from that point you read again the same values. You can use it for anything you want.
想象一下,BufferReader =“123456789”中有以下字符,如果您在相对于“5”字符的位置 4 进行标记,然后重置 BufferReader,最终将得到 12345。
Imagine you have the following chars in the the BufferReader = "123456789", if you mark in the position 4 relative to the '5' char then reset your BufferReader you will end up with 12345.
这是一个例子。
输出:
12341234
出于
readLimit
的目的,这里是一个很好的参考。Here's an example.
Output:
12341234
For the purpose of
readLimit
, here's a nice reference.