articles

Home / DeveloperSection / Articles / StreamTokenizer in Java

StreamTokenizer in Java

Anupam Mishra3765 16-Nov-2015
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.
import java.io.*;  
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  
     fout.write(b);  
    fout.close();  
    
   FileInputStream fin= new FileInputStream("abc.txt");
   InputStreamReader ir= new InputStreamReader(fin);
   StreamTokenizer st1 = new StreamTokenizer(ir);
   int token=0,count=0;
    while (true) {  
         token=st1.nextToken(); 
 if(token== StreamTokenizer.TT_EOF)
     break;
  if(token== StreamTokenizer.TT_NUMBER)
    System.out.println(st1.nval+" Number");
  if(token== StreamTokenizer.TT_WORD)
    System.out.println(st1.sval+" WORD");
                 count++;}  
   System.out.println("Numbers of Word in the File is :"+count);}  } 
StreamTokenizer in Java 


Updated 07-Sep-2019

Leave Comment

Comments

Liked By