Sunday, 20 July 2014

Java Program that displays the number of characters,lines and words in a given text file

Program

 import java.io.*;
 class count 
 {   
   public static void main(String args[]) throws IOException
   {
      int c,characters=0,lines=0,words=0;
      FileInputStream f=null
      try
      {
        f=new FileInputStream(args[0]);
      }
      catch(FileNotFoundException e){}
      while((c=f.read())!=-1)
      {
        characters++;
         if(c=='\n')
          lines++;
         if(c==' '||c=='\n'||c=='\t')
          words++;
      }
    System.out.println("Number of characters are "+characters);
    System.out.println("Number of lines are "+lines);
    System.out.println("Number of words are "+words);
   }
 }


--please leave a comment if it is useful to you

No comments:

Post a Comment