BufferedReader指针
我有以下代码,但我不明白如何将指针重置到起始位置:
BufferedReader inp=new BufferedReader(new FileReader(file));
Scanner leggi=new Scanner(inp);
for(int i=0;i<nwords;i++){
while(leggi.hasNext())
if(leggi.next().equals(args[i+2]))
occorrenze[i]=occorrenze[i]+1;
}
inp.close();
我尝试过
inp.mark(0);
inp.reset();
但没有结果。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Paul,
我建议您通读这个旧线程:Java BufferedReader back to the top of a text file?。
就我个人而言,我更喜欢乔恩·斯基特的回应,归根结底是“不要打扰[除非你必须]”。
干杯。基思.
编辑:此外,即使遇到异常,您也应该始终关闭该输入文件。
finally
块非常适合此目的。EDIT2:
希望您仍然和我们在一起。
这是我的尝试,FWIW,您不需要重置输入文件,您只需要转置
while
和for
循环。顺便说一句,java.util.Map非常适合构建频率表...并行数组只是 90 年代的事情。 Map 的“默认”实现是 HashMap 类...默认情况下,我的意思是只要您需要 Map 就使用 HashMap,除非您有充分的理由使用其他东西,而这不会经常。 HashMap 通常是最好的全能执行器。
Paul,
I suggest you read through this old thread: Java BufferedReader back to the top of a text file?.
Personally I prefer Jon Skeet's response, which boils down to "Don't bother [unless you MUST]."
Cheers. Keith.
EDIT: Also you should ALLWAYS close that input file, even if you hit an Exception. The
finally
block is perfect for this.EDIT2:
Hope you're still with us.
Here's my attempt, and FWIW, you DON'T need to reset the input-file, you just need to transpose your
while
andfor
loops.And BTW, java.util.Map is perfect for building a frequency table... Parallel arrays are just SOOOOO 90's. The "default" implementation of Map is the
HashMap
class... By default I mean use HashMap whenever you need a Map, unless you've got a good reason to use something else, which won't be often. HashMap is generally the best allround performer.您有两个选择:
FieldReader.reset
) 并创建一个新的 bufferedreader。我认为您尝试的是在读取输入后调用标记。相反,请执行以下操作:
You have two options:
FieldReader.reset
) instead and create a new bufferedreader.BufferedReader.markSupported
)I think what you tried is to call mark after you read the input. Instead do the following:
mark(int readAheadLimit )
接受一个readAheadLimit
参数。您不应将
readAheadLimit
设置为 0。尝试使用大于您在迭代中读取的字节数的有意义的数字。mark(int readAheadLimit)
takes areadAheadLimit
parameter.You should not set the
readAheadLimit
to 0. Try using a meaningful number that is larger than number of bytes you read in an iteration.