Count lines of a particular file

In this section, you will learn how to count the availability of text lines in the particular file. A file is read before counting lines of a particular file, . File is a collection of stored information that are arranged in string, rows, columns and li

In this program you will learn how to read a file and count number of lines in it. This is the simple program that opens a file and read one line at a time and count no of lines in a file.

Program first asks the user to enter the extension of the file and then it reads all the files with the input extension. Its reads the file(s) and then prints the no of lines in each file.

Java provides many API's for reading, writing, updating and deleting the file. You can also update the file content using Java programs.

We have used the following class in the program:

  • BufferedReader
  • InputStreamReader
  • FileReader
  • LineNumberReader

Here is the code of the program:


public class NumberOfLine{
	public static void main(String[] args) {
			System.out.println("Getting line number of a paritcular file example!");
			BufferedReader bf = new BufferedReader(new InputStreamReader(;
			System.out.println("Please enter file name with extension:");
			String str = bf.readLine();
			File file = new File(str);
			if (file.exists()){
				FileReader fr = new FileReader(file);
				LineNumberReader ln = new LineNumberReader(fr);
				int count = 0;
				while (ln.readLine() != null){
				System.out.println("Total line no: " + count);
				System.out.println("File does not exists!");
		catch(IOException e){

Download this example.

Use javac compiler to compile the file and to run the program you have use the java command on command prompt. 


C:\myexamples\Math_package>java NumberOfLine
Getting line number of a paritcular file example!
Please enter file name with extension:
Total line no: 58

Share on Google+Share on Google+

Count lines of a particular file

Posted on: March 13, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Count lines of a particular file   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
May 26, 2011

I have a project about Text details file ( String Type ) I want to learn the method and how use it Of count lines & size >>> Thanks
October 8, 2011
c programming

1)I need a program to count the number of lines in a file 2)program to count the number of words in a line
January 4, 2012
count number of lines in a file

count the statements which are: --->logical statements --->loops and end conditions --->initializations,declarations,directives,import statements Blank lines ,comment lines,or automatically generated code are not included in the count
santosha epili
May 2, 2012
code not visible

in this section the program code in not visible. plz update this page
August 11, 2012
how to write html code in java

How to write html code to a specific line in a file using java. Without altering content of the file. Example: line 1: has something line 2 : //content to be written here. line 3: has something Hope i can get some useful answers. Thanks
September 10, 2013

please visible the program code