Write a java program to count the characters, words and lines from file.

Here we write a java program which count the number of  characters, words and lines of a given file.The java class name is WordCOunt.java and you have to create a file which should be in the same folder of java class. See the example in below mentioned class.

package com.javaforecast4u;
import java.lang.*;
import java.io.*;
import java.util.*;
class WordCount{
     public static void main(String arg[]) throws Exception
        {
          int char_count=0;
          int word_count=0;
          int line_count=0;
          String s;
          StringTokenizer st;
          BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
          System.out.print("Enter filename : ");
          s=buf.readLine();
          buf=new BufferedReader(new FileReader(s));
          while((s=buf.readLine())!=null)
           {
            line_count++;
            st=new StringTokenizer(s," ,;:.");
            while(st.hasMoreTokens())
             {
               word_count++;
               s=st.nextToken();
               char_count+=s.length();
             }
          }
         System.out.println("Character Count : "+char_count);
         System.out.println("Word Count : "+word_count);
         System.out.println("Line Count : "+line_count);
         buf.close();
        }
}

 Note: the file should be same folder in which java class is there.

12 comments :

  1. I agree with your thoughts!!! As the demand of java programming application keeps on increasing, there is massive demand for java professionals in software development industries. Thus, taking training will assist students to be skilled java developers in leading MNCs.
    Java Training in Chennai

    ReplyDelete
  2. Thanks a lot for sharing this with all of us, I like it and we can communicate. Do you need buy app ratings and reviews. To boost app ranking and double app downloads now.

    ReplyDelete
  3. Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.


    sas training in bangalore

    ReplyDelete
  4. Thanks a lot very much for the high your blog post quality and results-oriented help. I won’t think twice to endorse to anybody who wants and needs support about this area. https://www.besanttechnologies.com/training-courses/data-warehousing-training/big-data-hadoop-training-institute-in-bangalore

    ReplyDelete
  5. Really it was an awesome article...very interesting to read..You have provided an nice article....Thanks for sharing..
    java training in chennai
    java course in chennai

    ReplyDelete