Blogroll

This blog is created for education about engineering students.All the engineering students are get free downloadable books here. not only books and also different software also available here

4.c. display the characters, lines and words in a FILE


c) Write a Java program that displays the number of characters, lines and words in a text file.
import javax.swing.*;
import java.io.*;
import java.util.*;
public class Count
{
public static void main(String args[])
{
try
{
String s=JOptionPane.showInputDialog("Enter the file name : ");
FileInputStream f=new FileInputStream(s);
DataInputStream d=new DataInputStream(f);
String data;
StringTokenizer st;
int words=0,chars=0,i=0;
while((data=d.readLine())!=null)
{
i++;
st=new StringTokenizer(data);
words+=st.countTokens();
chars+=data.length();
}
System.out.println("total words \n" +words);
System.out.println("total chras \n" +chars);
System.out.println("total lines \n" +i );
f.close();
}
catch(Exception)
System.out.println("err"+e);
}
}
}

No comments:

Post a Comment

Total Pageviews