The readLine Method in BufferedReader
You'll learn how readLine() reads an entire line of text at once, stopping at line breaks, and returns it as a convenient String.
What readLine() Does
The readLine() method in BufferedReader reads characters until it encounters a line terminator (like \n or \r\n), then returns everything it read as a String. The line terminators themselves are not included in the returned String—they're consumed and discarded.
When there's no more text to read (end-of-file), readLine() returns null instead of a String.
Why This Matters
Reading character by character is tedious. Most text files are organized into lines, and readLine() matches how we naturally think about text: one line at a time. It handles the messy details of different line-ending conventions across platforms for you.
How It Works in Practice
BufferedReader reader = new BufferedReader(
new FileReader("data.txt")
);
String line = reader.readLine(); // First line (or null if empty file)
String nextLine = reader.readLine(); // Second line (or null if no more)
The Null Signal
The null return value is your signal to stop reading:
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line); // Process each line
}
reader.close();
Think of readLine() like peeling layers off an onion—each call removes one complete layer (line) until nothing remains (null).
Empty Lines vs End-of-File
An empty line in a file returns an empty String "", not null. Only when the file has no more content does readLine() return null.
Key Takeaway: readLine() returns the next line as a String without line terminators, or null when the end of the file is reached—it's the standard way to process text files line by line.