对Reader进行token的类,可以访入多个split进行解析

作者:网络 来源:佚名 更新时间:2008-11-15 10:50:30 点击:

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

 }}