For the Reader to TOKEN, you can access multiple SPLIT for analysis.

xiaoxiao2021-03-05  47

In Java. The java.io.stringTokenizer class in Util can parse a single string, and the Split in JDK1.4 can also parse a single string, but you can't parse the Reader stream, when multiple characters are analyzed Can't return a string of two strings, the following classes are written in order to solve this problem, and can you include some of the most basic resolution types, * and?

You can set multiple splits for parsing, returns the corresponding split string when parsing

The program can run directly

/ * * Create a date 2005-4-4 * * Change the generated file template to * Window> Preferences> Java> Code Generation> Code and Comments * / 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 * * Change the template for the generated type annotation to * Window> Preferences> Java> Code Generation> Code and Note * / Public Class Readertoken Implements Iterator {

Private reader = null; // Storage Preparation Character Stream Private String [] splits = null; // Prepare to split the word StringBuffer Hasread = new stringbuffer (); // Store is already read from the stream Character private string [] splitsSF = NULL; // Since there may be some universalifiers, the characters when the formifier in TOKEN are stored, such as abcdefgh / ** * * / public readertoken (Reader Reader, String [] Split) {super (); this.reader = reader; this.splits = split; splitssf = new string [split.length]; for (int i = 0; i

/ * (Non-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.lend "; for (int i = 0; i

Try {while (true) {

For (int J = 0; j = Splits [j] .length ()) {currentsplit = j; return currentsplit;

// If the length of the current POS reaches the maximum value in the most splits, return to the current split}}

CHAR READCHAR = (char) Reader.Read ();

IF (READCHAR == -1) {Return Currentsplit;}

IF (readchar == 65535) {return currentsplit;}

Hasread.Append (Readchar);

For (int i = 0; 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)); // Get * behind a character, the general cord is expressed as 1 * 2 if (Nextchar == Readchar) {POS [i] ; POS [i] ;} splitsSSF [i] = readchar;} else if (splitposchar == '?') {POS [i] ; splitsSF [i] = readchar;} else {

POS [i] = 0; splitsSF [i] = ""

}

}

} catch (ioexception e) {

E.PrintStackTrace ();

Return -1;

/ * (Non-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;} ​​/ * (non-Javadoc) * @see Java.util.Iterator # remove () * / public void transove () {} 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.gettokenreader ());} system.rr.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); //}

}

转载请注明原文地址:https://www.9cbs.com/read-34564.html

New Post(0)