4

我有一个 BufferedReader 循环文件。当我遇到特定情况时,我想使用不同的阅读器实例继续循环,但从此时开始。

对推荐的解决方案有任何想法吗?创建单独的阅读器,使用标记功能等?

4

3 回答 3

2

在等待您对我的评论的答复时,我一直在做假设。

如果它是您重视的逐行输入,您可能会像我一样惊喜地发现RandomAccessFile现在(从 1.4 或 1.5 开始)支持该readLine方法。当然,RandomAccessFile 可以让您对位置进行细粒度控制。

如果您想要缓冲 IO,您可以考虑将阅读器包装在使用API映射的文件周围,CharacterBuffer或者包装在文件周围。这使您能够将文件视为内存,并对读取指针进行精细控制。而且由于数据都在内存中,缓冲是免费的。ByteBuffernio

于 2009-11-18T21:48:19.200 回答
1

你看过BufferedReader's 的mark方法吗?与它结合使用reset可能会满足您的需求。

于 2009-11-18T21:32:58.467 回答
0

If you keep track of how many characters you've read so far, you can create a new BufferedReader and use skip.

As Noel has pointed out, you would need to avoid using BufferedReader.readLine(), since readLine() will discard newlines and make your character count inaccurate. You probably shouldn't count on readLine() never getting called if anyone else will ever have to maintain your code.

If you do decide to use skip, you should write your own buffered Reader which will give you an offset counting the newlines.

于 2009-11-18T21:51:50.620 回答