Duplicate :
|
Name: ssT124754 Date: 02/26/2001 Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C) Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode) It struck me as odd several years ago that there is no InputStreamTokenizer in the java.util package. I see that as of yet it has not been added, and would like to suggest its addition as soon as possible. The code follows: package java.util; import java.io.InputStream; import java.io.IOException; public class InputStreamTokenizer { private InputStream in; private String delimit; private StringTokenizer tok; public InputStreamTokenizer(InputStream i, String del) { in = i; delimit = del; try { readData(); } catch (IOException e) { tok = new StringTokenizer("",""); } } private void readData() throws IOException { String buf = ""; byte b[] = new byte[1]; Character bb; if(in.available() > 0) { in.read(b); bb = new Character((char)b[0]); boolean keepit = !charIsDelimit(bb); while((buf.length() == 0 || keepit) && in.available() > 0) { if(keepit) buf += bb; in.read(b); bb = new Character((char)b[0]); keepit = !charIsDelimit(bb); } } tok = new StringTokenizer(buf, delimit); } private boolean charIsDelimit(Character bb) { for(int i=0; i<delimit.length(); i++) { if(bb.charValue() == delimit.charAt(i)) return true; } return false; } public boolean hasMoreTokens() { try { if(tok.hasMoreElements()) return true; else if(in.available() <= 0) return false; readData(); return tok.hasMoreElements(); } catch (IOException e) { System.out.println("Error reading stream: " + e); return false; } } public Object nextToken() throws NoSuchElementException { try { if(tok.hasMoreElements()) return tok.nextElement(); else if(in.available() <= 0) { throw new NoSuchElementException(); } readData(); return tok.nextElement(); } catch (IOException e) { throw new NoSuchElementException(); } } /* for test purposes */ public static void main(String args[]) { FileInputStream fin = null; try { fin = new FileInputStream(args[0]); } catch (FileNotFoundException e) { System.out.println("File not found: " + e); } InputStreamTokenizer tok = new InputStreamTokenizer(fin, " \n\t"); while(tok.hasMoreTokens()) { System.out.println("NEXT TOKEN:\t" + tok.nextToken()); } } } (Review ID: 117649) ======================================================================