File handling - Command Line Arguments - Line count

// File handling
// Command Line Arguments

/*
Write a Java program LineCounts.java that will count the number of lines in each files that is specified on the command line. Note that multiple files can be specified, as in               
    
     java LineCounts file1.txt file2.txt file3.txt
*/

import java.io.*;

class LineCounts
{
     public static void main(String as[]) throws IOException
     {
          for(int i=0;i<as.length;i++)
          {
              FileReader fr = new FileReader(as[i]);
              BufferedReader br = new BufferedReader(fr);
              int linecount=0;
              while(true)
              {
                   String str = br.readLine();
                   if(str==null)
                        break;
                  
                   linecount++;
              }
              fr.close();
              System.out.println(as[i]+" = "+linecount);
          }
     }
}

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu