程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Stream Tokenizing(分解字符串)

Stream Tokenizing(分解字符串)

編輯:關於JAVA

從sun網站看到的Stream Tokenizing

In Tech Tips: June 23, 1998, an example of string tokenization was presented, using the class Java.util.StringTokenizer.

There's also another way to do tokenization, using Java.io.StreamTokenizer. StreamTokenizer Operates on input streams rather than strings, and each byte in the input stream is regarded as a character in the range '\u0000' through '\u00FF'.

StreamTokenizer is lower level than StringTokenizer, but offers more control over the tokenization process. The class uses an internal table to control how tokens are parsed, and this syntax table can be modifIEd to change the parsing rules. Here's an example of how StreamTokenizer works:

import Java.io.*;

import Java.util.*;

public class streamtoken {

public static void main(String args[])

{

if (args.length == 0) {

System.err.println("missing input filename");

System.exit(1);

}

Hashtable Wordlist = new Hashtable();

try {

FileReader fr = new FileReader(args[0]);

BufferedReader br = new BufferedReader(fr);

StreamTokenizer st = new StreamTokenizer(br);

//StreamTokenizer st =

// new StreamTokenizer(new StringReader(

// "this is a test"));

st.resetSyntax();

st.WordChars('A', 'Z');

st.WordChars('a', 'z');

int type;

Object dummy = new Object();

while ((type = st.nextToken()) !=

StreamTokenizer.TT_EOF) {

if (type == StreamTokenizer.TT_Word)

Wordlist.put(st.sval, dummy);

}

br.close();

}

catch (IOException e) {

System.err.println(e);

}

Enumeration enum = Wordlist.keys();

while (enum.hasMoreElements())

System.out.println(enum.nextElement());

}

}

In this example, a StreamTokenizer is created on top of a FileReader / BufferedReader pair that represents a text file. Note that a StreamTokenizer can also be made to read from a String by using StringReader as illustrated in the commented-out code shown above (StringBufferInputStream also works, although this class has been deprecated).

The method resetSyntax is used to clear the internal syntax table, so that StreamTokenizer forgets any rules that it knows about parsing tokens. Then wordChars is used to declare that only upper and lower case letters should be considered to form Words. That is, the only tokens that StreamTokenizer recognizes are sequences of upper and lower case letters.

nextToken is called repeatedly to retrIEve words, and each resulting word is found in the public instance variable "st.sval". The words are inserted into a Hashtable, and at the end of processing the contents of the table are displayed, using an Enumeration as illustrated in Tech Tips: June 23, 1998. So the action of this program is to find all the unique Words in a text file and display them.

StreamTokenizer also has special facilitIEs for parsing numbers, quoted strings, and comments. It's a useful alternative to StringTokenizer, and is especially applicable if you are tokenizing input streams, or wish to exercise finer control over the tokenization process

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved