Write a Java program that displays the number of characters, lines and words in a text file. - Code

Latest

programs

Monday, October 10, 2011

Write a Java program that displays the number of characters, lines and words in a text file.


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