Home Java Example Java Io Count lines of a particular file
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Count lines of a particular file

Advertisement
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:

 
import java.io.*;

public class NumberOfLine{
	public static void main(String[] args) {
		try{
			System.out.println("Getting line number of a paritcular file example!");
			BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
			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){
					count++;
				}
				System.out.println("Total line no: " + count);
				ln.close();
			}
			else{
				System.out.println("File does not exists!");
			}
		}
		catch(IOException e){
			e.printStackTrace();
		}
	}
}

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>javac NumberOfLine.java

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


Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 13, 2008

Related Tutorials

Ask Questions?    Discuss: Count lines of a particular file   View All Comments

Post your Comment


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

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
vijay
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
raja
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
Alex
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
raju
September 10, 2013
java

please visible the program code
DMCA.com