在java。util中的java.io.StringTokenizer类能对单个字符串进行解析,在jdk1.4中的split也可以对单个字符串进行解析,但都不能对Reader流进行解析,在多个字符解析的时候也不能返回分割两个字符串的字符串,下面这个类就是为了解决这个问题而写的,还可以在准备包括一些最基本的解析类型,*和?
可以设置多个split进行解析,在解析的时候返回对应的分割字符串
程序可以直接运行
/* * 创建日期 2005-4-4 * * 更改所生成文件模板为 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释 */package com.csii.template;
import java.io.IOException;import java.io.InputStreamReader;import java.io.Reader;import java.io.StringReader;import java.util.Iterator;
/** * @author wsl * * 更改所生成类型注释的模板为 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释 */public class ReaderToken implements Iterator {
private Reader reader = null;//存放准备解析的字符流 private String[] splits = null;//准备用来分割字符流的token private StringBuffer HasRead = new StringBuffer();//存放从流中已经读的字符 private String[] splitsSf = null;//由于可能存在一些统配符,所以此处存放统配符在token时的字符,比如在abcdefgh /** * */ public ReaderToken(Reader reader, String[] split) { super(); this.reader = reader; this.splits = split; splitsSf = new String[split.length]; for (int i = 0; i < split.length; i++) { splitsSf[i] = ""; } }
/* (非 Javadoc) * @see java.util.Iterator#hasNext() */ public boolean hasNext() { if (indexOf() > -1) { return true; } return false; } private int currentsplit = -1; private int indexOf() {
currentsplit = -1; int[] pos = new int[splits.length]; for (int i = 0; i < pos.length; i++) { splitsSf[i] = ""; pos[i] = 0; } //初试位置符号
try { while (true) {
for (int j = 0; j < pos.length; j++) { if (pos[j] >= splits[j].length()) { currentsplit = j;
return currentsplit;
//如果当前pos里面的长度达到最splits里面的最大值,返回当前的split } }
char readchar = (char) reader.read();
if (readchar == -1) { return currentsplit; }
if (readchar == 65535) { return currentsplit; }
HasRead.append(readchar);
for (int i = 0; i < pos.length; i++) {
char splitposchar = splits[i].charAt(pos[i]);
if (splitposchar == readchar) { pos[i]++; splitsSf[i] += readchar; } else if (splitposchar == '*') { char nextchar = splits[i].charAt((pos[i] + 1)); //得到*后面一个字符,一般统配符都表示为1*2 if (nextchar == readchar) { pos[i]++; pos[i]++; } splitsSf[i] += readchar; } else if (splitposchar == '?') { pos[i]++; splitsSf[i] += readchar; } else {
pos[i] = 0; splitsSf[i] = ""; }
}
}
} catch (IOException e) {
e.printStackTrace(); }
return -1; }
/* (非 Javadoc) * @see java.util.Iterator#next() */ public Object next() {
int i = HasRead.length() - splitsSf[currentsplit].length();
StringBuffer sf = new StringBuffer(); sf = this.HasRead; HasRead = new StringBuffer(); // if(i<0){ // i=0; // } sf.setLength(i); return sf;
} public String getToken() { return splits[this.currentsplit]; } public String getTokenInReader() { return splitsSf[currentsplit]; } public Object getEnd() { return this.HasRead; } /* (非 Javadoc) * @see java.util.Iterator#remove() */ public void remove() {
} public static void main(String[] args) throws IOException { String[] a = new String[] { "a?a" }; String s = "cefaaasdbd"; ReaderToken token = new ReaderToken(new StringReader(s), a); while (token.hasNext()) { System.err.println("next---" + token.next()); System.err.println("token---" + token.getToken()); System.err.println( "getTokenInReader---" + token.getTokenInReader()); } System.err.println("end--" + token.getEnd());
// StringReader reader=new StringReader(s); // char c; // while((c=(char)reader.read())!=-1){ // System.err.println(c); // System.out.println((long)c); // }
}}