Computer Science, asked by Anonymous, 1 month ago

Write a program in java to get file name at runtime and display number of lines and words in that file.​

Answers

Answered by MrTSR
1

C0DE:

import java.util.*;

import java.io.*;

class Cfile

{

 public static void main(String args[])throws IOException

   {

       int nl=1,nw=0;

       char ch;

       Scanner scr=new Scanner(System.in);

       System.out.print("\nEnter File name: ");

       String str=scr.nextLine();

       FileInputStream f=new FileInputStream(str);

       int n=f.available();

       for(int i=0;i<n;i++)

       {

           ch=(char)f.read();

           if(ch=='\n')

               nl++;

         else if(ch==' ')

               nw++;

        }

       System.out.println("\nNumber of lines : "+nl);

       System.out.println("\nNumber of words : "+(nl+nw));

   }

}

THE 0UTPUT

Enter File name: Yoyo.txt

Number of lines : 1

Number of words : 5

Attachments:
Similar questions