JSPCN主页 | JSP空间 | 网站制作 | JSP下载 | JSP论坛 | JSP教程 | 关于JSPCN | 联系我们
JSP虚拟主机,jsp空间,java空间,java虚拟空间,详细请点击进入
做最专业的JSP中文网站 当前位置首页--JAVA技术--Applica  
文章搜索:
关键字 标题
  
JSP中文网内容管理系统(JCMS)
JSP虚拟主机
网络笔记本
网摘,图片,笔记收藏
虚拟服务器

JSPCN文章目录分类
JSP配置[219]JSP基础[136]
中文问题[69]上传问题[27]
JAVABEAN[46]数据库[212]
文件操作[126]图片声音[17]
JSP其他[57]时间相关[16]
JAVAMAIL[72]STRUTS[144]
开发工具[28]教程系列[157]
JSP实例[89]
JAVA基础[421]APPLET[78]
JAVA网络[179]Applica[115]
Servlet[98]XML[163]
J2ME[257]J2EE[374]
考试相关[63]JAVA线程[90]
EJB[261]Swing[26]
Java API[141]声音图片[28]
异常处理[33]JAVA实例[290]
JAVA类[139]SUN[89]
Hibernate[6]JMX[8]
Spring[34]
本版推荐文章 
本版热点文章 
相关文章链接 
 
Stream Tokenizing(分解字符串)
作者:未知     文章来源:www.jspcn.net
访问次数:331次     加入时间:2005年01月19日
作者:blackwhites
日期:2000-10-23 16:59:32
从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

 
JSP虚拟主机 | JSP空间 | JSP主机 | JSP双线虚拟主机 | JAVA空间 | JAVA虚拟主机 | 虚拟服务器 | JSP 虚拟服务器 | VPS
Virtual Private Server | JAVA虚拟服务器 | VM服务器 | VHOST | 虚拟操作系统 | JSP论坛 | JAVA论坛 | JSP站点论坛
Copyright © 2002-2005 JSPCN.net. All rights reserved.
JSP中文网    备案序号:蜀ICP备05001583号
成都恒海科技发展有限公司    成都市一环路南二段6号新瑞楼三楼8号