了解BufferedReader在Java中的工作方式 [英] Understanding how BufferedReader works in Java

查看:77
本文介绍了了解BufferedReader在Java中的工作方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于BufferedReader如何工作的非常基本的问题。给定字符串/短语,我想从其中包含很多文本的文件中查找并打印它。

Very basic question on how BufferedReader works. Given the string/phrase, I want to find and print it from the file with a lot of text in it.

在Java中使用BufferedReader 我对此主题进行了一些研究,结果是最接近的。

using BufferedReader in Java I did some research on this topic and that was the closest result. Not quite addressing my problem though.

因此,有了这些信息,为什么下面的代码会终止?

So with this information, why does the following code terminate?

public class MainApp {

String line = null;
String phrase = "eye";

try {
    File file = new File("text.txt");
    FileReader fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr);

    while((line = br.readLine()) != null) {
        if (line.equals(phrase) {
            System.out.println(line);
           }
        }

    br.close();

} catch (Exception e) {
   e.printStackTrace();
  }
 }
}

我对如何此代码块应该起作用:


  • while循环遍历文本的每一行,直到条件不再成立为止

  • 每行存储在 BufferedReader

  • 循环工作直到 if(line.equals(phrase )。

  • 打印找到的短语。

  • The while loop goes through each line of text until the condition is no longer true
  • Each line is stored in the BufferedReader
  • Loop is working until the condition of if (line.equals(phrase) is met.
  • Prints found phrase.

为什么我认为可能 不是 工作:


  • 读取行不会作为字符串存储在 BufferedReader 中(因此无法进行比较)

  • readlines are not stored as strings in the BufferedReader (therefore they can't be compared)

错误的逻辑(很可能是语句)

faulty logic (most likely the if statement)

为简单起见,我们假设 text.txt 里面充满了很长的传说ipsum,中间有一个眼睛 词。

For the sake of simplicity, let's assume that "text.txt" is filled with very long lore ipsum with a single "eye" word put somewhere in the middle of it.

问题出在哪里? (如果可能的话,请不要提供完整的代码解决方案,我很乐意为自己做代码工作)。

Where exactly is the problem? (Don't provide the entire code solution if possible, I'd love to do the coding part myself for the sake of practice).

推荐答案

您对该块应如何工作的理解:



  • while循环通过直到条件不再成立为止的每一行文本

更正:-)。



  • 每行都存储在 BufferedReader

不正确。 bufferedReader 不存储任何数据,它只是读取数据(这就是为什么它被称为 bufferedReader 的原因)。当您调用 br.readLine()时,它将为您提供包含行内容的字符串,但它本身不会存储任何内容。在您的情况下,每一行都存储在 line 变量中,该变量在每次循环运行时都会被覆盖。

Not correct. The bufferedReader does not store any data, it just reads it (Thats why it's called bufferedReader). When you call br.readLine() it will give you a String with the contents of the line, but it will not store anything itself. In your case, each line is stored in the line variable, which is overwritten each time the loop runs.



  • 循环工作直到满足 if(line.equals(phrase)。)的条件。

  • Loop is working until the condition of if (line.equals(phrase) is met.

不正确。即使满足条件,循环也将继续工作。如果要停止循环,则需要插入 break 语句。在您的情况下,当满足条件 时,它将打印整行,并且循环将继续进行;对于您而言,该语句可能永远不会得到满足,因为 if(line.equals(phrase)可能永远都不是。

Not correct. The loop will continue working even if the condition is met. If you want the loop to stop, you need to insert a break statement. In your case, when the condition is met, it will print the whole line, and the loop will continue. In your case the statement will probably never be met, because the if (line.equals(phrase) will probably never be true.



  • 打印找到的短语。

可能的话,如果 whole 行等于该短语。该短语被其他单词包围,条件(line.equals(phrase)将不成立。

Possibly, if the whole line equals the phrase. If the phrase is surrounded by other words, the condition (line.equals(phrase) will not be true.

为什么您认为可能 不是 工作:



  • 读取行是不能作为字符串存储在 BufferedReader 中(因此无法进行比较)

  • readlines are not stored as strings in the BufferedReader (therefore they can't be compared)

如上所述, BufferedReader 中没有存储任何内容。您将每一行存储在 line 变量中。然后将您与变量进行比较。

As I said above, nothing is stored inside the BufferedReader . You are storing each individual line in the line variable. Then you are comparing with the line variable.



  • 错误的逻辑(

是。 if 语句中的条件是错误的,因为它检查 whole 行是否与所需短语匹配。此外,即使找到该短语,循环也将继续运行。

Yes. The condition in the if statement is wrong, since it checks if the whole line matches the wanted phrase. Also, the loop will continue running, even if the phrase is found.


为简单起见,我们假设 text.txt ; 充满了很长的知识点ipsum,并在其中途放置了一个眼睛 单词。

For the sake of simplicity, let's assume that "text.txt" is filled with very long lore ipsum with a single "eye" word put somewhere in the middle of it.

在这种情况下,您的代码可能不会显示任何内容。

In this case, your code will probably not print anything.


问题出在哪里? (如果可能的话,请不要提供完整的代码解决方案,为了练习起见,我很乐意自己做编码部分)

Where exactly is the problem? (Don't provide the entire code solution if possible, I'd love to do the coding part myself for the sake of practice)

问题是在循环的条件下。悬停在此处以查看其状态:

The problem is in the condition for the loop. Hover here to see how it should be:


if(line.contains(phrase))

此外,循环中没有 break 语句,因此它将如果文件中存在该短语,则多次打印该短语。 (并且如果循环的条件是固定的!)

Also, there is no break statement in the loop, so it will print the phrase multiple times, if it exists in the file. (And if the condition for the loop is fixed!)

这篇关于了解BufferedReader在Java中的工作方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆