read file content line by line java program

The readLine() method is used to get file content line by line. The readLine() returns one line at each iteration.

Example:

package com.w3schools;
 
import java.io.BufferedReader;
import java.io.FileReader;
 
public class ReadFile {
  public static void main(String args[]){
	 String fileName = "D:/Test files/file 3.txt";
	 BufferedReader br = null;
     String strLine = "";
     try {
         br = new BufferedReader( new FileReader(fileName));
         while( (strLine = br.readLine()) != null){
             System.out.println(strLine);
         }
     } catch (Exception e) {
         e.printStackTrace();
     } 
  }
}

Output:

Content file 3

Download this example.