在Java中使用BufferedReader重置缓冲区?
我正在使用 BufferedReader 类在缓冲区中逐行读取。当读取缓冲区中的最后一行时,我想再次从缓冲区的开头开始读取。 我已经阅读过有关 mark()
和 reset()
的内容,我不确定它的用法,但我认为它们不能帮助我解决这个问题。
有谁知道如何在到达最后一行后从缓冲区的开头开始读取?就像我们可以使用 RandomAccessFile
的 seek(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
标记/重置是您想要的,但是您不能真正在 BufferedReader 上使用它,因为它只能重置回一定数量的字节(缓冲区大小)。如果你的文件比这个大,它就不起作用。没有“简单”的方法可以做到这一点(不幸的是),但它并不太难处理,您只需要原始 FileInputStream 的句柄。
(注意,不建议使用默认字符集,仅使用简化示例)。
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.
(note, using default character sets is not recommended, just using a simplified example).
是的,标记和重置是您想要使用的方法。
Yes, mark and reset are the methods you will want to use.
这对我解决这个问题很有帮助。我正在逐行读取文件。我很早就在程序中做了一个 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。
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.