Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Create a program which prints number of lines in the file

  1. /*
  2. Create a program which prints number of lines in the file. File name
  3. should be supplied using command line argument.
  4. */
  5.  
  6. import java.io.*;
  7. class CountNumberOfLinesFile
  8. {
  9.  public static void main(String args[])
  10.  {
  11.   try
  12.   {
  13.    FileReader fr = new FileReader(args[0]);
  14.    BufferedReader br = new BufferedReader(fr);
  15.    String line;
  16.    int count = 0;
  17.    
  18.    while((line = br.readLine()) != null){
  19.     count++;
  20.    }
  21.    
  22.    System.out.println("There are " + count + " line(s) in the file.");
  23.    fr.close();
  24.   }
  25.   catch(Exception e)
  26.   {
  27.    System.out.println("Exception : " + e);
  28.   }
  29.  
  30.  }
  31. }
No votes yet
Share this

Post new comment