by Dinesh Thakur

 

import java.io.*;

class CountingLinesJavaExample
{
         public static void main(String args[]) throws IOException
        {
             String t;
             int l,w,c,n,i;
             BufferedReader fin = new BufferedReader(new FileReader(args[0]));
             l=w=c=0;
             while(true)
                  {
                     try
                        {
                             t= fin.readLine();
                             if (t==null)
                             break;
                             l++;
                             n = t.length();
                             for (i=0;i<=n-1;i++)
                                  {
                                     if(t.charAt(i)==' ')w++;
                                     else
                                     c++;
                                  }
                                     w++;
                                     System.out.println(t);
                        }
                                    catch(IOException e){}
                 }
                System.out.println("The Number of lines are "+l);
                System.out.println("The Number of lines are "+w);
                System.out.println("The total number of characters are"+c);
        }
 }

Counting Lines,words,Char in a File Java Example