java - 使用 BufferedReader 读取部分文件

标签 java bufferedreader

我正在尝试编写一个函数,该函数获取文件的特定部分,将其发送到另一个函数,然后从 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/

相关文章:

java - Spring Boot - Maven 构建错误

java - Spring Boot如何在服务器重启时轮换日志文件

java - Android 停止线程 onDestroy

java.io.InputStreamReader.ready() 阻止执行

java - 编译 java 文件并将文件传递到 stdin 会出现错误

java - 读取大文件(例如非常大的文本文档)的最佳方式

java - 在Java中读取csv时删除第一行

java - 如何制定下载文件的方法?

java - 从 1-50 的生成器生成 1-100 的随机数

java - 如何在 Java 1.4 中为 BufferedReader 和 PrintWriter 设置超时?