Java Program To Count Number of Lines In The File

In this Java program, we will learn to count the number of lines in a text file. In Java, you can count the number of lines present in a file using the BufferedReader class, which reads text from a character-input stream and provides a convenient method called readLine() which reads a line of text.


Java Program to Count Number of Lines in File Using BufferedReader

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

public class FileLineCountExample {
  public static void main(String[] args) {
    String fileName = "example.txt";
    int lineCount = 0;
    try (BufferedReader reader = new BufferedReader(
            new FileReader(fileName))) {
      while (reader.readLine() != null) {
        lineCount++;
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    System.out.println("Line count: " + lineCount);
  }
}
Output
Line count: 1

In this example, the main method first creates a BufferedReader object and uses it to read the file named "example.txt" line by line. The readLine() method returns a string containing the next line of text from the file, or null if the end of the file has been reached. The while loop iterates over the file, and for each iteration, it increases the lineCount variable by 1. Finally, it prints the total number of lines in the file to the console.

It's important to note that the BufferedReader class also has a read() method, which reads a single character at a time, but it's not practical for counting lines in a file because it's very slow and it doesn't handle new line characters.

Another way to count the number of lines in a file is by using the Files class from the java.nio.file package, which provides a method called lines() that reads all the lines of a file into a stream and returns the number of elements in that stream.


Java Program to Count Number of Lines in File Using Files Class

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class FileLineCountExample {
  public static void main(String[] args) {
    String fileName = "example.txt";
    long lineCount = 0;
    try {
      lineCount = Files.lines(Paths.get(fileName)).count();
    } catch (IOException e) {
      e.printStackTrace();
    }
    System.out.println("Line count: " + lineCount);
  }
}
Output
Line count: 1

In this example, the main method first calls the Files.lines(Paths.get(fileName)) method to read all the lines of the file named "example.txt" into a stream. Then the count() method is called on that stream, which returns the number of lines in the file. The lineCount variable is used to store the number of lines and it's printed to the console.

It's important to note that, similar to the previous example, this method also assumes that the file exists, and the program has the permission to read the file, if the file doesn't exist, or the program doesn't have the permission the code will throw an exception.

This way also ensures that the file is closed after it's used and it's a more efficient way to count the number of lines in a file, because it reads the file in a lazy way, only reading the lines that are needed.