For reader to token class, you can visit multiple split to parse

Source: Internet
Author: User
Tags object return string split window stringbuffer

In Java. The Java.io.StringTokenizer class in Util can parse a single string, split in jdk1.4 can parse a single string, but cannot parse the reader stream, nor can you return a string that splits two strings when multiple character parsing, the following class is To solve this problem, it can also be prepared to include some of the most basic analytic types, * and?

You can set up multiple split to parse and return the corresponding split string when parsing

Programs can be run directly

* * Creation Date 2005-4-4 * * Change the generated file template for * window > Preferences > Java > Code generation > Codes and annotations */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 Annotations */public class Readertoken implements Ite Rator {

Private Reader reader = null;//store ready to parse the character stream private string[] splits = null;//Prepare token private stringbuffer for dividing character streams hasread = New StringBuffer ()//hold characters that have been read from the stream private string[] SPLITSSF = null;//because of the possible presence of wildcard characters, the wildcard character is stored here, as in 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] = ""; } }

/* (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.length];   for (int i = 0; i < pos.length i++) {Splitssf[i] = "";  Pos[i] = 0; }//Preliminary position symbol

try {while (true) {

for (int j = 0; J < Pos.length J + +) {if (Pos[j] >= splits[j].length ()) {currentsplit = J;

return currentsplit;

If the length within the current POS reaches the maximum splits inside, return 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 < 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));       To get a character followed by the *, General wildcard characters are expressed as 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; }

/* (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[curr Entsplit]; The public Object Getend () {return this. Hasread; }/* (non-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); //  }

}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.