File Information Java Program

This program take a file path and file name in the command line and displays the details of the file. This is a part of Mumbai University MCA Colleges Java Programs sem 4.


Program to accepts a filename from command line argument and displays the number of characters, words, and lines in the file.



import java.io.*;
class CountF
{
public static void main(String args[])throws IOException
{
 int ch;
 boolean prev=true;
 int char_count=0;
 int word_count=0;
 int line_count=0;
 FileInputStream fin=new FileInputStream(args[0]);
 while((ch=fin.read())!=-1)
 {
   if(ch!=' ') ++char_count;
   if(!prev && ch==' ') ++word_count;
   if(ch==' ') prev=true;else prev=false;
   if(ch=='\n') ++line_count;
 }
 char_count -= line_count*2;
 word_count += line_count;
 System.out.println("Number of Characters="+char_count);
 System.out.println("Number of Words="+word_count);
 System.out.println("Number of Lines="+line_count);
 fin.close();
 }
}


Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

No comments:

Post a Comment