In this tutorial how to get details of a file using StreamTokenizer class in Core Java is shown.
Code:
StreamTokenizerDemo.java
import java.io.*;
public class StreamTokenizerDemo
{
public static void main(String args[]) throws IOException
{
FileReader freader = new FileReader(“Demo.txt”);
StreamTokenizer st = new StreamTokenizer(freader);
double sum=0;
int numWords=0,numChars=0;
while(st.nextToken() != st.TT_EOF)
{
if(st.ttype == StreamTokenizer.TT_NUMBER)
{
sum+=st.nval;
}
else if(st.ttype == StreamTokenizer.TT_WORD)
{
numWords++;
numChars+=st.sval.length();
}
}
System.out.println(“Sum of total number in the file: “+sum);
System.out.println(“Total words in the file: “+numWords);
System.out.println(“Number of characters available in words: “+numChars);
}
}