0% found this document useful (0 votes)
24 views

Countfile - Java: S.No: 20 Date: 2022-06-15

This document describes a Java program that counts the number of characters, words, and lines in a text file. The program uses classes like Scanner, BufferedReader, and FileReader to read in a text file specified by the user. It then parses the file line by line and word by word to tally the character, word, and line counts, which it prints out at the end. The document includes the source code and sample text files to test the program.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Countfile - Java: S.No: 20 Date: 2022-06-15

This document describes a Java program that counts the number of characters, words, and lines in a text file. The program uses classes like Scanner, BufferedReader, and FileReader to read in a text file specified by the user. It then parses the file line by line and word by word to tally the character, word, and line counts, which it prints out at the end. The document includes the source code and sample text files to test the program.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

S.No: 20 Exp. Name: Count characters, words and lines in a file.

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
{

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

Scanner sc=new Scanner (System.in);

System.out.print("Enter file name(text1.txt or text2.txt): ");

String fname=sc.next();

int cno=0;

int lno=0;

int wno=0;

BufferedReader br=null;

try

br=new BufferedReader(new FileReader(fname));

    Rajeev Gandhi Memorial College of Engineering and Technology (Autonomous)      2019-2023-MECH-FDH


}

catch(FileNotFoundException fe)

System.out.println("File not found.");

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

Save Our Environment by Being Responsible:

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.

It is a very precious element of our environment.

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

    Rajeev Gandhi Memorial College of Engineering and Technology (Autonomous)      2019-2023-MECH-FDH


the nature to save the earth for our own future.

Execution Results - All test cases have succeeded!

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.

You might also like