我正在尝试编写一个函数,该函数获取文件的特定部分,将其发送到另一个函数,然后从 BufferedReader 停止的位置继续执行相同的操作,直到文件末尾,但似乎无法弄清楚如何让它发挥作用。
这是我所拥有的:
String str = "";
int count = 0;
try {
while(//condition so it loops through the entire file. I've tried fileReader.ready() and fileReader.read != -1 but both just run into infinite loops){
while ((count < 4)){
str += fileReader.read();
count++;
fileReader.mark(1000);
}
fileReader.reset();
DoSomething(str) // send str to another function and do something with it;
}
} catch (IOException e) {
// TODO Auto-generated catch block
}
有人可以帮我解决这个问题并解释我做错了什么吗?非常感谢
最佳答案
如果您知道字符数,请使用 BufferedReader 的 .skip(long)
方法,它告诉它跳过前一个 long
字符(其中 long
是 64 位整数)。
对skip的调用将返回一个long
,其中包含实际跳过的字符数。
关于java - 使用 BufferedReader 读取部分文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4168937/