Friday, June 8, 2012

Write a program to get a file name at run time and display number of lines and words in that file.?


♣♣♣ WRITE A PROGRAM TO GET A FILE NAME AT RUN TIME AND DISPLAY NUMBER OF LINES AND WORDS IN THAT FILE. ♣♣♣


import java.lang.*;
import java.io.*;

class pro14
{
        public static void main(String ar[]) throws IOException
        {
        InputStreamReader a=new InputStreamReader(System.in);
        BufferedReader b=new BufferedReader(a);
        int lines=0,words=0;
        System.out.print("\n\tEnter The File Name : ");
        String s=b.readLine();
        File f=new File(s);
        InputStreamReader in=new InputStreamReader(new FileInputStream(f));
                        StreamTokenizer st=new StreamTokenizer(in);
                                                                        st.wordChars(0,255);
                                        st.whitespaceChars(0,' ');
                                        st.eolIsSignificant(true);
                                        while(st.nextToken()!= st.TT_EOF)
                                        {
                                                if(st.ttype==st.TT_EOL)
                                                            lines++;
                                                if(st.ttype==st.TT_WORD)
                                                    words++;
                                        }
             System.out.println("\n\tNumber of Lines : "+lines+"\n\t Number of words : "+words);

        }
}


OUTPUT :-
                                   
                        Enter The File Name : c:\test.txt
                       
                        Number of Lines : 0
                        Number of Words : 2

                        Enter The File Name : c:\hook.log
                       
                        Number of Lines : 238
                        Number of Words : 357

No comments:

Post a Comment