Программирование на Java


Исходный текст приложения


Исходный текст приложения StreamToken представлен в листинге 1.

Листинг 1. Файл StreamToken.java

import java.io.*;

public class StreamToken { public static void main(String args[]) { DataOutputStream OutStream; DataInputStream InStream;

byte bKbdInput[] = new byte[256]; String sOut;

try { System.out.println( "Enter string to parse...");

System.in.read(bKbdInput);

sOut = new String(bKbdInput, 0);

OutStream = new DataOutputStream( new BufferedOutputStream( new FileOutputStream( "output.txt")));

OutStream.writeBytes(sOut); OutStream.close();

InStream = new DataInputStream( new BufferedInputStream( new FileInputStream( "output.txt")));

TokenizerOfStream tos = new TokenizerOfStream();

tos.TokenizeIt(InStream); InStream.close();

System.out.println( "Press <Enter> to terminate..."); System.in.read(bKbdInput); } catch(Exception ioe) { System.out.println(ioe.toString()); } } }

class TokenizerOfStream { public void TokenizeIt(InputStream is) { StreamTokenizer stok; String str;

try { stok = new StreamTokenizer(is); stok.slashSlashComments(true);

stok.ordinaryChar('.');

while(stok.nextToken() != StreamTokenizer.TT_EOF) { switch(stok.ttype) { case StreamTokenizer.TT_WORD: { str = new String( "\nTT_WORD >" + stok.sval); break; }

case StreamTokenizer.TT_NUMBER: { str = "\nTT_NUMBER >" + Double.toString(stok.nval); break; }

case StreamTokenizer.TT_EOL: { str = new String("> End of line"); break; }

default: { if((char)stok.ttype == '"') { str = new String( "\nTT_WORD >" + stok.sval); }

else str = "> " + String.valueOf( (char)stok.ttype); } }

System.out.println(str); } } catch(Exception ioe) { System.out.println(ioe.toString()); } } }




Начало  Назад  Вперед



Книжный магазин