Using the StringReader class : StringReader « File « Java Tutorial






import java.io.StreamTokenizer;
import java.io.StringReader;

public class Main {
  public static void main(String[] args) throws Exception{
    StringReader reader = new StringReader("this is a test");

    int wordCount = 0;
    StreamTokenizer streamTokenizer = new StreamTokenizer(reader);

    while (streamTokenizer.nextToken() != StreamTokenizer.TT_EOF) {
      if (streamTokenizer.ttype == StreamTokenizer.TT_WORD)
        wordCount++;
    }
    System.out.println("Number of words in file: " + wordCount);
  }
}
//Number of words in file: 4








11.35.StringReader
11.35.1.Using the StringReader class
11.35.2.Create StringReader from a string
11.35.3.Create BufferedReader from StringReader