BufferedReader 中的标记和重置是什么? [英] What are mark and reset in BufferedReader?

查看:82
本文介绍了BufferedReader 中的标记和重置是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道BufferedReadermark()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.

推荐答案

流的 markreset 方法提供了一种在流中向后跳转并重新读取数据.

The mark and reset methods of streams provide a way to jump backwards in the stream and re-read data.

当您在 BufferedReader 上调用 mark() 时,它将开始将您从该点读取的数据保存在其内部缓冲区中.当您调用 reset() 时,它将跳回到流的标记位置,因此内存中的缓冲区将满足接下来的 read() .当您读过该缓冲区的末尾时,它将无缝地返回读取新数据.BufferedInputStream 以同样的方式工作.

When you call mark() on a BufferedReader it will begin keeping data you read from that point forwards in its internal buffer. When you call reset() it will jump back to the marked position of the stream, so the next read()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.

mark 的 int 参数告诉它您想要的最大字符数(对于 BufferedReader)或字节数(对于 BufferedInputStream)能够倒退.如果读取的数据超过标记位置过多,则标记可能会失效",调用 reset() 将失败并抛出异常.

The int parameter to mark tells it the maximum number of characters (for BufferedReader) or bytes (for BufferedInputStream) 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 calling reset() will fail with an exception.

一个小例子:

BufferedReader r = new BufferedReader(new StringReader(
    "Happy Birthday to You!
" +
    "Happy Birthday, dear " + System.getProperty("user.name") + "!"));
r.mark(1000); // save the data we are about to read
System.out.println(r.readLine()); // read the first line
r.reset(); // jump back to the marked position
r.mark(1000); // start saving the data again
System.out.println(r.readLine()); // read the first line again
System.out.println(r.readLine()); // read the second line
r.reset(); // jump back to the marked position
System.out.println(r.readLine()); // read the first line one final time

在那个例子中,我将 StringReader 包裹在一个 BufferedReader 中以获得 readLine() 方法,但是 StringReadercode>s 本身已经支持 markreset !从 in-memory 数据源读取的流通常支持 markreset 本身,因为它们已经在内存中拥有所有数据,所以它是便于他们再次阅读.从文件或管道或网络套接字读取的流自然不支持 markreset,但您始终可以通过将其包装在 中来将该功能添加到任何流中BufferedInputStreamBufferedReader.

In that example, I wrapped the StringReader in a BufferedReader to get the readLine() method, but StringReaders already support mark and reset on their own! Streams that read from an in-memory data source usually support mark and reset 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 support mark and reset, but you can always add that feature to any stream by wrapping it in a BufferedInputStream or BufferedReader.

这篇关于BufferedReader 中的标记和重置是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆