Java - File handling - line, word and character count


// File handling

/*
Write a Java program that displays the number of characters, lines and words in a text file.
*/

import java.io.*;

class FileLWC
{
     public static void main(String as[]) throws IOException
     {
         
          FileInputStream fin = new FileInputStream("ipt.txt");
          int lc = 0, wc = 0, cc = 0;
         
          while(true)
          {
              int i = fin.read();
              if(i==-1)
              {
                   lc++;
                   wc++;
                   break;
              }
              char c = (char)i;
             
              if(c=='\n' || c=='.')
              {
                   lc++;
                   wc++;
              }
              else if(c=='\t' || c==' ')
                   wc++;
              else if((c>='a' && c<='z')||(c>='A' && c<='Z'))
                   cc++;
                            
          }       
          System.out.println("Line count = "+lc);
          System.out.println("Word count = "+wc);
          System.out.println("Character count = "+cc);
          fin.close();
     }
}

No comments:

Post a Comment

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

Anu