Write a program in java to get file name at runtime and display number of lines and words in that file.
Answers
Answered by
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
Science,
21 days ago
Accountancy,
21 days ago
Environmental Sciences,
21 days ago
Chemistry,
1 month ago
Hindi,
9 months ago