Thursday, July 19, 2018

How to Count Lines in a File in Java

Sometimes you just want to count the number of lines in a file, rather than reading the content of file. The easiest way, I feel, is to use LineNumberReader for counting the lines in a file in Java.

LineNumberReader class has a method getLineNumber() that gives the current line number of the file. So the logic for counting the number of lines in a file is as follows-

Using the LineNumberReader read all the lines of the files until you reach the end of file. Then use getLineNumber() method to get the current line number.

Using the getLineNumber() method you can also display line numbers along with the lines of the file.

Java program to count number of lines in a file

If you have a file with lines as follows-

This is a test file.
Line number reader is used to read this file.
This program will read all the lines.
It will give the count.

Then you can get the count of lines using the following code-

 
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

public class LineNumberDemo {
  public static void main(String[] args) {
    LineNumberReader reader = null;
    try {
      reader = new LineNumberReader(new FileReader(new File("F:\\abc.txt")));
      // Read file till the end
      while ((reader.readLine()) != null);
      System.out.println("Count of lines - " + reader.getLineNumber());
    } catch (Exception ex) {
      ex.printStackTrace();
    } finally { 
      if(reader != null){
        try {
          reader.close();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }
  }
}

Output

 
Count of lines – 4

Printing lines of the file with line number

If you want to print lines of the file along with the line number you just need to tweak the above code a little.

 
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

public class LineNumberDemo {
 public static void main(String[] args) {
  LineNumberReader reader = null;
    try {
      reader = new LineNumberReader(new FileReader(new File("F:\\abc.txt")));
      String str;
      // Read file till the end
      while ((str = reader.readLine()) != null){
        System.out.println(reader.getLineNumber() + "- " + str);
      }         
    } catch (Exception ex) {
      ex.printStackTrace();
    } finally { 
      if(reader != null){
        try {
          reader.close();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }
  }
}

Output

 
1- This is a test file.
2- Line number reader is used to read this file.
3- This program will read all the lines.
4- It will give the count.

That's all for this topic How to Count Lines in a File in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page


Related Topics

  1. Reading Delimited File in Java Using Scanner
  2. Reading File in Java 8
  3. How to Read File From The Last Line in Java
  4. Zipping Files in Java
  5. How to Create PDF From XML Using Apache FOP

You may also like-

  1. How to Create Password Protected Zip File in Java
  2. Print odd-even numbers using threads and wait-notify
  3. Converting double to int - Java Program
  4. Getting All The Tables in a Schema in a DB - Java Program
  5. abstract class in Java
  6. Java Collections Interview Questions And Answers
  7. Java Stream API Tutorial
  8. interface static methods in Java 8