StreamTokenizer in Java

It is provide various fields like double nval (the value of a number tokens.), sval (the string given the character of a word token), and various constant fields. 

Now we taken an example, firstly we write content in a file with the help of FileOutputStream (Let’s take ‘abc.txt’.). Then, read that content to find which one is number and which one is word and also be find total numbers of words in a file.
public class StreamTokenizerEx{  
 public static void main(String args[])throws IOException{  
    FileOutputStream fout=new FileOutputStream("abc.txt"); 
 String s="India is Transforming in coming 20 years in future. Now In 2015, india is rapidly growing country in this world";  
     byte b[]=s.getBytes();//converting string into byte array  
   FileInputStream fin= new FileInputStream("abc.txt");
   InputStreamReader ir= new InputStreamReader(fin);
   StreamTokenizer st1 = new StreamTokenizer(ir);
   int token=0,count=0;
    while (true) {  
 if(token== StreamTokenizer.TT_EOF)
  if(token== StreamTokenizer.TT_NUMBER)
    System.out.println(st1.nval+" Number");
  if(token== StreamTokenizer.TT_WORD)
    System.out.println(st1.sval+" WORD");
   System.out.println("Numbers of Word in the File is :"+count);}  } 
StreamTokenizer in Java 

Last updated:9/7/2019 12:16:01 AM


Leave Comment