Countfile - Java: S.No: 20 Date: 2022-06-15
Countfile - Java: S.No: 20 Date: 2022-06-15
Date: 2022-06-15
ID: 20095A0363
Page No:
Aim:
Write a Java program that displays the number of characters, lines and words in a text file.
Source Code:
CountFile.java
import java.io.*;
import java.util.*;
class CountFile
{
String fname=sc.next();
int cno=0;
int lno=0;
int wno=0;
BufferedReader br=null;
try
catch(FileNotFoundException fe)
String str;
while((str=br.readLine())!=null)
lno++;
String words[]=str.split(" ");
for(String s:words)
cno=cno+s.length();
wno=wno+words.length;
System.out.println("Lines: "+lno);
System.out.println("Words: "+wno);
System.out.println("Characters: "+cno);
br.close();
text1.txt
Your every action will count. You should not only hold others responsible, make
yourself responsible too.
Why not start saving our environment being a little less self-concerned. Sometim
es give priority to the
ID: 20095A0363
Page No:
nature before giving priority to yourself. Save the energy, save plants and be s
ympathetic to the nature
surrounding us.
Life priorities and necessities are never going to reduce. But among all of the
m, make some time for ensuring
the well being of the environment you live in. To save our environment, no life
changing movement is required.
If anything is required, that is will power, honest inclination and some small i
nitiatives. Save our
environment by being a responsible citizen. Teach your child and others to save
water. Do not waste water.
text2.txt
So we should try to save our environment by making the small day to day initiati
ves.
The first thing you should do is try to save water, trees and electricity. This
will
make a big difference. Also try to spread good words and educate children about
it. Love
Test Case - 1
User Output
Enter file name(text1.txt or text2.txt): text1.txt
Lines: 12
Words: 140
Characters: 715
Test Case - 2
User Output
Enter file name(text1.txt or text2.txt): Text1.txt
File not found.