Tutorialspoint examples

Learn java tutorial with examples for beginners online

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.tutorialspointexamples;
 
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.

Download tutorialspointexamples Android App
Copyright © 2018 Tutorialspoint examples DMCA.com Protection Status