【问题标题】:Go to next line in a file with BufferedReader使用 BufferedReader 转到文件中的下一行
【发布时间】:2019-11-08 18:22:39
【问题描述】:

我正在尝试从文件中读取行。为此,我使用以下代码:

try {
    String line;
    try (InputStream fis = new FileInputStream("AbsoluteFilePath");
        InputStreamReader isr = new InputStreamReader(fis, Charset.forName("Cp1252"));
        BufferedReader br = new BufferedReader(isr);) {
      FactGeneration.getFacts();
      while ((line = br.readLine()) != null) {
        br.readLine();
        function1(line);

但是,这不会移动到文件中的下一行。 谢谢!

编辑: 为了清楚起见,我正在制作一个 twitter 机器人。整个函数如下所示: FactGeneration.getFacts() 向位于 /AbsoluteFilePath 的文件追加一个新行

private static void tweetLines() {
String tweet;
int count = 0;
while (count < 10) {
  try {
    try (InputStream fis = new FileInputStream(
        "/AbsoluteFilePath");
        InputStreamReader isr = new InputStreamReader(fis, Charset.forName("Cp1252"));
        BufferedReader br = new BufferedReader(isr);) {
      FactGeneration.getFacts();
      while ((tweet = br.readLine()) != null) {
        sendTweet(tweet);

        try {
          int sleepTime = 18000;
          Thread.sleep(sleepTime);
        }
        catch (InterruptedException e) {
          e.printStackTrace();
        }

      }
    }
  }
  catch (IOException e) {
    e.printStackTrace();
  }
  count += 1;
}

}

编辑:

以下是工作代码:

while (count < numTweets) {
  try {
    try (InputStream fis = new FileInputStream(fileName);
        InputStreamReader isr = new InputStreamReader(fis, Charset.forName("Cp1252"));
        BufferedReader br = new BufferedReader(isr)) {


      //Calls static method GetFacts from the FactGeneration class.
      FactGeneration.getFacts();
      tweet = br.readLine();
      while (tweet != null) {

        //sendTweet accesses the TwitterAPI and posts the tweet.
        sendTweet(tweet);
        System.out.println("tweeting:" + tweet);
        try {
          //Pauses the thread for the given amount of time.
          int sleepTime = 18000; //in milliseconds.
          System.out.printf("Sleeping for %d seconds", sleepTime / 1000);
          Thread.sleep(sleepTime);
        }
        catch (InterruptedException e) {
          e.printStackTrace();
        }
        //moves to the next line in the file.
        tweet = br.readLine();
      }
    }
  }
  catch (IOException e) {
    e.printStackTrace();
  }
  count += 1;
  if (count == numTweets) {
    System.out.println("Tweet limit reached");
  }
}

【问题讨论】:

  • 您在显示该行之前调用了两次readLine()。为什么?
  • @KarolDowbecki 我只是在搞乱代码,试图让它工作。最初,我在 while 循环的头部只有一个 readLine()。

标签: java file bufferedreader twitter4j


【解决方案1】:

readLine 实际上读取一行,它在while 循环的条件内执行,所以不要在循环内读取第二次

String line;
try (InputStream fis = new FileInputStream("AbsoluteFilePath");
     InputStreamReader isr = new InputStreamReader(fis, Charset.forName("Cp1252"));
     BufferedReader br = new BufferedReader(isr);) {

    while ((line = br.readLine()) != null) {
        // do whatever with line

    }
}

while 条件有 2 个步骤,这允许在最后读取 null 并停止循环

  • 从文件中读取一行并将结果赋值给line
  • 检查line 不为空

【讨论】:

  • 即使将其更改为使用单个 readLine(),它也不会移动到下一行。
  • @fragElephant 所以问题出在其他地方,您是否尝试仅打印该行?
  • @fragElephant 请您现在考虑接受答案或发表评论;)
  • 抱歉耽搁了,问题在于代码中 readLine() 的位置。我正在用工作代码更新答案。谢谢!
猜你喜欢
  • 2012-05-08
  • 1970-01-01
  • 2012-03-14
  • 1970-01-01
  • 2015-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-12
相关资源
最近更新 更多