在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); // }
}}