Java BufferedReader 回到文本文件的顶部? [英] Java BufferedReader back to the top of a text file?

查看:40
本文介绍了Java BufferedReader 回到文本文件的顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在同一个文本文件上初始化了 2 个 BufferedReader .当我用第一个 BufferedReader 读取完文本文件后,我使用第二个从顶部再次通过文件.需要多次通过同一个文件.

I currently have 2 BufferedReaders initialized on the same text file. When I'm done reading the text file with the first BufferedReader, I use the second one to make another pass through the file from the top. Multiple passes through the same file are necessary.

我知道 reset(),但它需要在调用 mark() 之前,并且 mark() 需要知道文件的大小,我认为我不应该打扰.

I know about reset(), but it needs to be preceded with calling mark() and mark() needs to know the size of the file, something I don't think I should have to bother with.

想法?包裹?图书馆?代码?

Ideas? Packages? Libs? Code?

谢谢TJ

推荐答案

只创建一个新的 BufferedReader 从顶部读取有什么缺点?如果文件足够小,我希望操作系统缓存文件.

What's the disadvantage of just creating a new BufferedReader to read from the top? I'd expect the operating system to cache the file if it's small enough.

如果您担心性能,您是否证明它是一个瓶颈?我只会做最简单的事情,除非你有特定的理由,否则我不会担心.我的意思是,您可以将整个内容读入内存,然后对结果进行两次传递,但同样,这比使用新阅读器从头开始读取要复杂得多.

If you're concerned about performance, have you proved it to be a bottleneck? I'd just do the simplest thing and not worry about it until you have a specific reason to. I mean, you could just read the whole thing into memory and then do the two passes on the result, but again that's going to be more complicated than just reading from the start again with a new reader.

这篇关于Java BufferedReader 回到文本文件的顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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