在Java中使用BufferedReader重置缓冲区?

发布于 2024-10-26 02:37:57 字数 253 浏览 4 评论 0原文

我正在使用 BufferedReader 类在缓冲区中逐行读取。当读取缓冲区中的最后一行时,我想再次从缓冲区的开头开始读取。 我已经阅读过有关 mark()reset() 的内容,我不确定它的用法,但我认为它们不能帮助我解决这个问题。

有谁知道如何在到达最后一行后从缓冲区的开头开始读取?就像我们可以使用 RandomAccessFileseek(0) 一样?

I am using class BufferedReader to read line by line in the buffer. When reading the last line in the buffer, I want to start reading from the beginning of the buffer again.
I have read about the mark() and reset(), I am not sure its usage but I don't think they can help me out of this.

Does anyone know how to start reading from the beginning of the buffer after reaching the last line? Like we can use seek(0) of the RandomAccessFile?

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

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

发布评论

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

评论(3

行雁书 2024-11-02 02:37:57

标记/重置是您想要的,但是您不能真正在 BufferedReader 上使用它,因为它只能重置回一定数量的字节(缓冲区大小)。如果你的文件比这个大,它就不起作用。没有“简单”的方法可以做到这一点(不幸的是),但它并不太难处理,您只需要原始 FileInputStream 的句柄。

FileInputStream fIn = ...;
BufferedReader bRead = new BufferedReader(new InputStreamReader(fIn));

// ... read through bRead ...

// "reset" to beginning of file (discard old buffered reader)
fIn.getChannel().position(0);
bRead = new BufferedReader(new InputStreamReader(fIn));

(注意,不建议使用默认字符集,仅使用简化示例)。

mark/reset is what you want, however you can't really use it on the BufferedReader, because it can only reset back a certain number of bytes (the buffer size). if your file is bigger than that, it won't work. there's no "simple" way to do this (unfortunately), but it's not too hard to handle, you just need a handle to the original FileInputStream.

FileInputStream fIn = ...;
BufferedReader bRead = new BufferedReader(new InputStreamReader(fIn));

// ... read through bRead ...

// "reset" to beginning of file (discard old buffered reader)
fIn.getChannel().position(0);
bRead = new BufferedReader(new InputStreamReader(fIn));

(note, using default character sets is not recommended, just using a simplified example).

ま柒月 2024-11-02 02:37:57

是的,标记和重置是您想要使用的方法。

// set the mark at the beginning of the buffer
bufferedReader.mark(0);

// read through the buffer here...

// reset to the last mark; in this case, it's the beginning of the buffer
bufferedReader.reset();

Yes, mark and reset are the methods you will want to use.

// set the mark at the beginning of the buffer
bufferedReader.mark(0);

// read through the buffer here...

// reset to the last mark; in this case, it's the beginning of the buffer
bufferedReader.reset();
望她远 2024-11-02 02:37:57

这对我解决这个问题很有帮助。我正在逐行读取文件。我很早就在程序中做了一个 BufferedReader 。然后,我检查 readLine 是否为 null 并执行 myFile.close,然后执行新的 BufferedReader。第一次通过时,readLine 变量将为 null,因为我在全局范围内设置了它,然后还没有执行 readLine。该变量是全局定义的并设置为 null。结果,一个关闭的新的 BufferedReader 发生了。如果我不在程序的一开始就执行 BufferedReader,那么 myFile.close 在第一次传递时会抛出 NPE。

当文件逐行读取时,此测试失败,因为 readLine 不为空,测试中没有任何反应,文件解析的其余部分继续。

稍后,当 readLine 到达 EOF 时,它的值再次为 null。 IE:第二次通过此检查还会执行 myFile.close 和新的 BufferedREader,它将 readLine 重新重置回开头。

实际上,在循环内部或循环外部,此操作仅在 readLine 变量全局设置为 null 或 EOF 时发生。无论哪种情况,我都会执行“重置”以返回到文件的开头和新的 BufferedReader。

if (readLineOutput == null) { 
//end of file reached or the variable was just set up as null
    readMyFile.close();
    readMyFile = new BufferedReader(new FileReader("MyFile.txt"));
                }

This worked for me to resolve this problem. I'm reading in a file line by line. I'm doing a BufferedReader very early in my program. I then check if the readLine is null and perform a myFile.close and then a new BufferedReader. The first pass through, the readLine variable will be null since I set it that way globally and then haven't done a readLine yet. The variable is defined globally and set to null. As a result, a close and new BufferedReader happens. If I don't do a BufferedReader at the very beginning of my program, then this myFile.close throws an NPE on the first pass.

While the file is reading through line by line, this test fails since the readLine is not null, nothing happens in the test and the rest of the file parsing continues.

Later, when the readLine gets to EOF it is valued as null again. IE: The second pass through this check also does a myFile.close and new BufferedREader which resets the readLine back to the beginning again.

Effectively, inside my loop or outside my loop, this action only happens at the readLine variable being globally set to null or at EOF. In either case I do a "reset" to get back to the beginning of the file and a new BufferedReader.

if (readLineOutput == null) { 
//end of file reached or the variable was just set up as null
    readMyFile.close();
    readMyFile = new BufferedReader(new FileReader("MyFile.txt"));
                }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文