Java Tutorial

 
 
 
 
import java.io.*;

class  countChar
{
    public static void main(String[] args) throws IOException
    {
           File f = new File(args[0]);
           FileOutputStream fos = new FileOutputStream(f);
           for (int i=0;i<5;i++ )
           {
                 fos.write((int)(Math.random()*4));
           }
           fos.close();

           FileInputStream fis = new FileInputStream(f);
           int ch;
           int c[] = new int[5];
           for (int i=0;i<c.length ;i++ )
           {
                 c[i]=0;
           }
           while((ch=fis.read())!=-1) {
                  switch (ch)
                  {
                  case 0: 
                                 c[0]=c[0]+1;
                                 break;
                  case 1:
                                 c[1]=c[1]+1;                
                                 break;
                  case 2:
                                 c[2]=c[2]+1;                  
                                 break;
                  case 3:
                                 c[3]=c[3]+1;                               
                                 break;
                 case 4:
                                 c[4]=c[4]+1;
                                 break;
                  }
                 System.out.println(ch);
            }
            System.out.println(\"\\n\\n\\nCounting of Characters\\n\");
            for (int i=0;i<c.length ;i++ )
            {
                 System.out.println(\"count for \"+i+\"    : \" +c[i]);
            }
            fis.close();
    }
}

 
 
Didn't find what you were looking for? Find more on File handling program to count number of characters in a file