Java - BufferedReader readLine() method



Description

The Java BufferedReader readLine() method read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

Declaration

Following is the declaration for java.io.BufferedReader.readline() method.

public String readline()

Parameters

NA

Return Value

A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached.

Exception

IOException − If an I/O error occurs.

Assumption

Assuming we have a text file example.txt, which has the following content. This file will be used as an input for our example programs −

ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz

Example - Using readLine() method

The following example shows the usage of Java BufferedReader readLine() method.

BufferedReaderDemo.java

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

public class BufferedReaderDemo {
   public static void main(String[] args) throws Exception {
      String thisLine = null;
      
      InputStream is = null; 
      InputStreamReader isr = null;
      BufferedReader br = null;
      try {

         // open input stream example.txt for reading purpose.
         is = new FileInputStream("example.txt");

         // create new input stream reader
         isr = new InputStreamReader(is);

         // create new buffered reader
         br = new BufferedReader(isr);

         while ((thisLine = br.readLine()) != null) {
            System.out.println(thisLine);
         }       
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz

Example - Reading Lines from a String

The following example shows the usage of Java BufferedReader readLine() method.

BufferedReaderDemo.java

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;

public class BufferedReaderDemo {
   public static void main(String[] args) {
      String input = "Hello, World!\nThis is a test.\nBufferedReader Example.";

      // Initialize BufferedReader with a StringReader
      try (BufferedReader reader = new BufferedReader(new StringReader(input))) {
         String line;

         // Read and print each line
         while ((line = reader.readLine()) != null) {
            System.out.println(line);
         }
      } catch (IOException e) {
         System.err.println("An error occurred: " + e.getMessage());
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Hello, World!
This is a test.
BufferedReader Example.

Explanation

  • The BufferedReader is created with a StringReader that contains multiple lines of input.

  • The readLine() method reads one line at a time, stopping at line terminators (\n).

  • When the end of the stream is reached, readLine() returns null, ending the loop.

  • The program prints each line of the input string.

Example - Counting the Number of Lines

The following example shows the usage of Java BufferedReader readLine() method.

BufferedReaderDemo.java

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;

public class BufferedReaderDemo {
   public static void main(String[] args) {
      String input = "Line 1: Java Programming\nLine 2: BufferedReader\nLine 3: readLine Example";

      // Initialize BufferedReader with a StringReader
      try (BufferedReader reader = new BufferedReader(new StringReader(input))) {
         String line;
         int lineCount = 0;

         // Count the number of lines
         while ((line = reader.readLine()) != null) {
            lineCount++;
            System.out.println("Line " + lineCount + ": " + line);
         }

         System.out.println("Total number of lines: " + lineCount);
      } catch (IOException e) {
         System.err.println("An error occurred: " + e.getMessage());
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Line 1: Line 1: Java Programming
Line 2: Line 2: BufferedReader
Line 3: Line 3: readLine Example
Total number of lines: 3

Explanation

  • The BufferedReader reads a string containing multiple lines of text.

  • The readLine() method is used inside a loop to read one line at a time.

  • A counter (lineCount) increments for each line read, keeping track of the total number of lines.

  • The program prints each line with its corresponding line number and the total number of lines at the end.

Key Points About readLine()

  • Reads a Line− The method reads one line of text and stops at line terminators (\n, \r, or \r\n).

  • Return Value− Returns the line as a String or null when the end of the stream is reached.

  • Efficient for Line-Based Input− Ideal for reading text files or multi-line strings line by line.

java_io_bufferedreader.htm
Advertisements