BufferedReader.skip() 相当于行,而不是字符
Java中是否有某种相当于BufferedReader.skip()的东西,它将行数而不是字符数作为参数?
我想跳到文本文件中的特定行并从该点开始读取,而不需要遍历文件的所有行并检查行号(数以万计的行号 - 模型 obj 文件)。
我看到的所有示例都在处理行号检查,这不是我想要的。
Is there in Java some sort of equivalent to BufferedReader.skip(), that would take number of lines as parameter instead of number of characters?
I want to jump to a specific line in a text file and start reading from that point without the need going thru all the lines of the file and checking against the line number (tens of thousands of them - model obj file).
All the examples I saw were dealing with the checking of line number which is not what I want.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,解决方案是使用 FileInputStream.skip()。
更新: 在每次行迭代时手动将系统特定的新行分隔符字节长度添加到行字节长度解决了错误字节跳过的问题,所以现在它终于按预期工作了!
定义一些
Long
变量,用于存储要跳过的字节数。我在我的主应用程序类(App.class)中做到了这一点:然后,在您使用 BufferedReder 读取行的方法/函数中,使其像这样(我读取的所有文件都编码为UTF-8):
So, the solution is to use
FileInputStream.skip()
.UPDATE: manually adding system-specific new line separator bytes length to line bytes length at each line iteration solved the problem of erroneous bytes skipping, so now it finally works as expected!
Define some
Long
variable where you will store the number of bytes to skip. I did that in my main application class (App.class):Then, in your method/function where you read your lines with
BufferedReder
make it like this (all my files that I read from are encoded as UTF-8):