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.
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();
}
}
}
My understanding of how this block should work:
- 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.
Why I think it might not work:
readlines are not stored as strings in the BufferedReader (therefore they can't be compared)
faulty logic (most likely the if statement)
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).
Your understanding of how this block should work:
Correct :-) .
Not correct. The
bufferedReader
does not store any data, it just reads it (Thats why it's calledbufferedReader
). When you callbr.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 theline
variable, which is overwritten each time the loop runs.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 theif (line.equals(phrase)
will probably never be true.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.Why you think it might not work:
As I said above, nothing is stored inside the
BufferedReader
. You are storing each individual line in theline
variable. Then you are comparing with theline
variable.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.In this case, your code will probably not print anything.
The problem is in the condition for the loop. Hover here to see how it should be:
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!)Your code should work. The
BufferedReader Class
just read buffers of data from the stream. It just means it does not read byte by byte from the file (which would take an eternity to execute).What the
BufferedReader Class
will do is read a buffer of bytes from the file (1024 bytes for instance). It will look for a line separator ("\n") in the buffer. If not found, the bytes will be appended in aStringBuilder
object and the next buffer will be fetched. This will happen until a line separator is found in the buffer. All bytes in the buffer up until the line separator will be appended to theStringBuilder
object, and finally the String will be returned to you.Edit: depending on implementation, the line separator might or might not be included in the String. Other people pointed out
contains()
, however, it would be a lot slower. If you want to find a specific line, do it withequals()
(Add the line separator in the phrase String). If you want to find a specific phrase within a line, thencontains()
is the way to go.When you read a line it will also contain all other data on the line
To correctly find if that line contains 'eye' you should call
contains()
instead ofequals()
you need to use the line.contains method, not the line.equals which you are currently using
so it is what you're saying "faulty logic (most likely the if statement)"
then you can print the line (or whatever you want to do)
if the line is the following :
it will not match although it contains the eye which you want to capture.. so change the if as I mentioned and you are good to go