Get Total Number Lines File Java

Get Total Number Lines File Java

In this sample java program you will see efficient way to get total number of lines from file using java program:

  • CheckTotalNumberOfLines.java
package com.javahonk;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class CheckTotalNumberOfLines {

	public static void main(String[] args) {
		
		try {
			int lines = 0;
			BufferedReader reader = new BufferedReader(new FileReader("C:\\JavaHonk\\Log4jXML.log"));
			while (reader.readLine() != null) lines++;
			reader.close();			
			System.out.println("Total nmber of lines: "+lines);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
  • Output:

Get Total Number Lines File Java

  • For more information on buffer reader please refer oracle documentation here

Leave a Reply

Your email address will not be published. Required fields are marked *