Understanding how BufferedReader works in Java

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 a StringBuilder 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 the StringBuilder 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 with equals() (Add the line separator in the phrase String). If you want to find a specific phrase within a line, then contains() is the way to go.


you need to use the line.contains method, not the line.equals which you are currently using

if (line.contains(phrase)) { 

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)

System.out.println(s);

if the line is the following :

Lorem ipsum dolor sit amet, **eye** consectetur adipiscing elit.

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