JAVA CODE FOR STREAM CUSTOMIZATION-COUNT READER | URDINESH

Software Programming, Tutorials, Interview Preparations,Stock Market,BSE/NSE, General informations

Tuesday, May 20, 2014

JAVA CODE FOR STREAM CUSTOMIZATION-COUNT READER



PROGRAM:

import java.io.*;
public class Streamcustomization extends Reader
{
            Reader in=null;
int  read=0,charCount=0,wordCount=0,lineCount=0;
            boolean whiteSpace=true;
            public void CountReader(Reader r)
            {
                        in=r;
            }
            public int read(char[]array,int off,int len)throws IOException
            {
                        if(array==null)
                        throw new IOException("null array");
                        read=in.read(array,off,len);
                        charCount+=read;
                        char c;
                        for(int i=0;i<read;i++)
                        {
                                    c=array[i];
                                    if(c=='\n')
                                    lineCount++;
                                    if(Character.isWhitespace(c))
                                                whiteSpace=true;
                                    else if(Character.isLetterOrDigit(c) && whiteSpace)
                                    {
                        wordCount++;
                        whiteSpace=false;
                                    }
                        }
                        return read;
            }
            public void close() throws IOException
            {
                        in.close();
            }
            public int getCharCount()
            {
                        return charCount;
            }
           

public int getWordCount()
            {
return wordCount;
            }
            public int getLineCount()
            {
                        return lineCount;
            }
            public static void main(String a[]) throws Exception
            {
                        CountReader cr=new CountReader(new FileReader("lab.java"));
                        char c[]=new char[4096];
                        int read=0;
                        while((read=cr.read(c,0,c.length))!=-1)
                        System.out.println(new String(c,0,read));
                        cr.close();
System.out.println("\n\n Read chars:"+cr.getCharCount()+"\n words:"+cr.getWordCount()+"\n lines:"+cr.getLineCount());
            }
 }



OUTPUT:

class lab
{
public static void main(String args[])
{
System.out.println("welcome to MCA lab");
}
}


 Read chars:18
 words:4

 lines:1

No comments:

Post a Comment

Thanks for your valuable comments

Followers