i modified the count word part and got the desired output
/*
pgm to count the characters in a file,no. of lines in a file and no. of words
*/
import java.io.*;
public class countCharacters
{
public void Lines()throws IOException
{
File f = new File("in.dat");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String str = br.readLine();
LineNumberReader ln = new LineNumberReader(br);
int count = 0;
while (ln.readLine()!=null)
{
count++;
}
System.out.println("no. of lines in the file = " + count);
}
public void Words()throws IOException
// count no. of words.
{
File f = new File("in.dat");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
StreamTokenizer stz = new StreamTokenizer(br);
int index = 0;
int numWords =0;
while(index !=StreamTokenizer.TT_EOF )
{
index =stz.nextToken();
numWords++;
}
System.out.println("no. of words in file = " + numWords);
}
public void Charact()throws IOException
// counting characters
{
File f = new File("in.dat");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
long numChar = f.length();
int countChar =0;
while(countChar<numChar)
{
countChar++;
}
System.out.println("no. of characters in the file =" + countChar);
// return countChar;
}
public static void main(String args[])throws IOException
{
countCharacters ob1 = new countCharacters();
ob1.Charact();
ob1.Words();
ob1.Lines();
}
}
Last edited by cscgal; Sep 2nd, 2008 at 10:49 pm. Reason: Fixed code tags