BufferedReader 中的标记和重置是什么?

发布于 2024-12-17 11:58:14 字数 120 浏览 0 评论 0原文

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

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

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

发布评论

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

评论(6

临风闻羌笛 2024-12-24 11:58:14

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

当您在 BufferedReader 上调用 mark() 时,它将开始将您从该点向前读取的数据保留在其内部缓冲区中。当您调用 reset() 时,它将跳回流的标记位置,因此内存缓冲区将满足下一个 read() 的要求。当您读取超过该缓冲区末尾时,它将无缝返回读取新数据。 BufferedInputStream 的工作方式相同。

mark 的 int 参数告诉它您希望能够读取的最大字符数(对于 BufferedReader)或字节数(对于 BufferedInputStream)倒退。如果您读取了超过标记位置的太多数据,则标记可能会“无效”,并且调用 reset() 将失败并出现异常。

一个小例子:

BufferedReader r = new BufferedReader(new StringReader(
    "Happy Birthday to You!\n" +
    "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() 方法,但是 StringReader< /code> 已经支持自己的标记重置!从内存中数据源读取的流通常支持markreset本身,因为它们已经在内存中拥有了所有数据,所以它是他们很容易再次阅读它。从文件、管道或网络套接字读取的流自然不支持 markreset,但您始终可以通过将其包装在 中来将该功能添加到任何流中BufferedInputStream 或 BufferedReader。

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

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.

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.

A little example:

BufferedReader r = new BufferedReader(new StringReader(
    "Happy Birthday to You!\n" +
    "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

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.

﹏半生如梦愿梦如真 2024-12-24 11:58:14

mark() 标记流中的特定点,reset() 将流重置为最新标记。这些方法提供了书签功能,允许您在流中提前阅读以检查即将到来的数据。

从这个文档:

ma​​rk()方法在输入中标记一个位置,流可以通过以下方式“重置”到该位置:
调用reset()方法。参数readLimit是读取的数量
之前设置标记后可以从流中读取的字符
该标记无效。例如,如果使用 read 调用 mark()
限制为 10,那么当从流中读取 11 个字符的数据时
在调用reset()方法之前,则该标记无效并且
流对象实例不需要记住标记。注意
通过该方法可以记住的字符数可以是
大于内部读缓冲区的大小。也不是
依赖于支持标记/重置的从属流
功能。

The mark() marking a particular point in a stream and reset() resets the stream to the most recent mark. These methods provide a book-marking feature that allows you to read ahead in the stream to inspect the upcoming data.

From this documentation:

The mark() method mark a position in the input to which the stream can be "reset" by
calling the reset() method. The parameter readLimit is the number of
chars that can be read from the stream after setting the mark before
the mark becomes invalid. For example, if mark() is called with a read
limit of 10, then when 11 chars of data are read from the stream
before the reset() method is called, then the mark is invalid and the
stream object instance is not required to remember the mark. Note that
the number of chars that can be remembered by this method can be
greater than the size of the internal read buffer. It is also not
dependent on the subordinate stream supporting mark/reset
functionality.

久夏青 2024-12-24 11:58:14

Reader::mark(int readLimit)文档说:

在此阅读器中设置标记位置。参数readLimit表示
在标记失效之前可以读取多少个字符。
调用reset()会将读卡器重新定位回标记位置
如果未超过 readLimit。

示例:

import java.io.*;
import static java.lang.System.out;

public class App {

    public static final String TEST_STR = "Line 1\nLine 2\nLine 3\nLine 4\n";

    public static void main(String[] args) {

        try (BufferedReader in = new BufferedReader(new StringReader(TEST_STR))) {

            // first check if this Reader support mark operation
            if (in.markSupported()) {

                out.println(in.readLine());
                in.mark(0);                     // mark 'Line 2'
                out.println(in.readLine());
                out.println(in.readLine());
                in.reset();                     // reset 'Line 2'
                out.println(in.readLine());
                in.reset();                     // reset 'Line 2'
                out.println(in.readLine());
                in.mark(0);                     // mark 'Line 3'
                out.println(in.readLine());
                in.reset();                     // reset 'Line 3'
                out.println(in.readLine());
                out.println(in.readLine());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

输出:

Line 1
Line 2
Line 3
Line 2
Line 2
Line 3
Line 3
Line 4

Reader::mark(int readLimit) documentation say:

Sets a mark position in this reader. The parameter readLimit indicates
how many characters can be read before the mark is invalidated
.
Calling reset() will reposition the reader back to the marked position
if readLimit has not been surpassed.

Example:

import java.io.*;
import static java.lang.System.out;

public class App {

    public static final String TEST_STR = "Line 1\nLine 2\nLine 3\nLine 4\n";

    public static void main(String[] args) {

        try (BufferedReader in = new BufferedReader(new StringReader(TEST_STR))) {

            // first check if this Reader support mark operation
            if (in.markSupported()) {

                out.println(in.readLine());
                in.mark(0);                     // mark 'Line 2'
                out.println(in.readLine());
                out.println(in.readLine());
                in.reset();                     // reset 'Line 2'
                out.println(in.readLine());
                in.reset();                     // reset 'Line 2'
                out.println(in.readLine());
                in.mark(0);                     // mark 'Line 3'
                out.println(in.readLine());
                in.reset();                     // reset 'Line 3'
                out.println(in.readLine());
                out.println(in.readLine());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output:

Line 1
Line 2
Line 3
Line 2
Line 2
Line 3
Line 3
Line 4
久隐师 2024-12-24 11:58:14

阅读器界面让您返回,您只能阅读。另一方面,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.

眼前雾蒙蒙 2024-12-24 11:58:14

想象一下,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.

许你一世情深 2024-12-24 11:58:14

这是一个例子。

int bufferSize = 4;
int readLimit = 4
ByteArrayInputStream byteInputStream = new ByteArrayInputStream("123456789abcdef".getBytes());
try(BufferedInputStream bufferedInputStream = new BufferedInputStream(byteInputStream, bufferSize)) {
        bufferedInputStream.mark(readLimit);
        System.out.print((char) bufferedInputStream.read());//byte1
        System.out.print((char) bufferedInputStream.read());//byte2
        System.out.print((char) bufferedInputStream.read());//byte3
        System.out.print((char) bufferedInputStream.read());//byte4
        bufferedInputStream.reset();
        System.out.print((char) bufferedInputStream.read());//byte5
        // Using this next reset() instead of the first one will throw an exception
        // bufferedInputStream.reset();

        System.out.print((char) bufferedInputStream.read());
        System.out.print((char) bufferedInputStream.read());
        System.out.print((char) bufferedInputStream.read());
    }

输出:12341234

出于readLimit的目的,这里是一个很好的参考。

Here's an example.

int bufferSize = 4;
int readLimit = 4
ByteArrayInputStream byteInputStream = new ByteArrayInputStream("123456789abcdef".getBytes());
try(BufferedInputStream bufferedInputStream = new BufferedInputStream(byteInputStream, bufferSize)) {
        bufferedInputStream.mark(readLimit);
        System.out.print((char) bufferedInputStream.read());//byte1
        System.out.print((char) bufferedInputStream.read());//byte2
        System.out.print((char) bufferedInputStream.read());//byte3
        System.out.print((char) bufferedInputStream.read());//byte4
        bufferedInputStream.reset();
        System.out.print((char) bufferedInputStream.read());//byte5
        // Using this next reset() instead of the first one will throw an exception
        // bufferedInputStream.reset();

        System.out.print((char) bufferedInputStream.read());
        System.out.print((char) bufferedInputStream.read());
        System.out.print((char) bufferedInputStream.read());
    }

Output: 12341234

For the purpose of readLimit, here's a nice reference.

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