CS411X - Lecture 2

homeContentsIndexPrevNext

Lecture Topic:

Tokenizers contd...

The first time tokens.nextToken( ) is called, the 1st token is loaded.
Note: space & carriage returns are not tokens.

static public void write_filtered_href_file(String input_name, String output_name) { 
    System.out.println("Filtering:\t" + input_name +"\t>\t"+output_name);
    try {
        FileInputStream input_stream = new FileInputStream(input_name);
        StreamTokenizer tokens = new StreamTokenizer(input_stream);
        FileOutputStream output_stream = new FileOutputStream(output_name);
        PrintStream output = new PrintStream(output_stream);
        int i;
        int next = 0;
        tokens.resetSyntax();
        tokens.wordChars(0,255);
        tokens.quoteChar('"');
        while ((next = tokens.nextToken()) != tokens.TT_EOF) {
               switch (next) {
                case '"':
                     output.print('"');
                     for (i=0;i<tokens.sval.length();i++) {
                        if (tokens.sval.charAt(i) == ' ') 
                            {output.print("%20");}
                        else
                            {output.print(tokens.sval.charAt(i));}
                     } //end for
                     output.print('"');
                     break;
                case tokens.TT_WORD: 
                     output.print(tokens.sval+" ");
                     break;
                case tokens.TT_NUMBER:
                     output.print(tokens.nval+" "); 
                     break;
                   case tokens.TT_EOL:
                     output.println();
                     break;
                } // end switch
               } // end while
           input_stream.close();
           output_stream.close();
    } // end try
    catch (Exception exe) 
        {System.out.println("list_filtered_href_file:er!");}
  } // write_filtered_hrefs
    

tokens.sval returns a string.
Note: When you are done, close the file

        FileInputStream input_file = new 
                     FileInputStream(file);
                 StreamTokenizer tokens = new StreamTokenizer(input_file);
 
                 while ((next = tokens.nextToken()) != tokens.TT_EOF) 
                 {
                     switch (next) {
                        case '"':
                            System.out.print('"');
                            StringTokenizer st = 
                                new StringTokenizer(tokens.sval," ");
                            while (st.hasMoreTokens()) {
                                System.out.print(st.nextToken());
                                if (st.countTokens() > 1) 
                                    {System.out.print("%20");}
                            }
                            System.out.print('"');
                            break;
                        case tokens.TT_WORD: 
                            System.out.print(tokens.sval+" ");
                            break;
                        case tokens.TT_NUMBER:
                            System.out.print(tokens.nval+" "); 
                            break;
                        case tokens.TT_EOL:
                            System.out.println();
                            break;
                     }
                 }
                 System.out.println();
                 input_file.close();
        }

old_file : my file
new_file: my%20file

How to Write Files ?

        FileOutputStream output_stream = new FileOutputStream(output_name);
           output_stream.close();

Given a PrintStream instance you can write string to the stream using
print & println methods.

Summary :

import java.io.* ;
FileOutputStream fOS = new FileOutputStream( filestream );
PrintStream pS = new PrintStream( fOS );
pS.print("I am writing to a file");
pS.println("Just like System.out");
fOS.close( );

MenuBarFrame.java


import java.awt.*;
import java.io.*;
 
class MenuBarFrame extends Frame  {
    MenuBar menuBar;
    String str;
    
    private int JavaIdentifiersMayBeOfUnlimitedLengthTheycanhavenoSpacesNoOperatorsTheyCANHave1234567890NumbersOrDigitsButTheyMustStartWithLetters = 0;
 
    private String science_items [] = {
            "Image Processing","-",
            "Audio Processing",
            "surface",
            "benchmark",
            "-",
            "Toy Calculator"
    };

geometry.java


import java.util.*;
import java.awt.*;
import java.io.*;
abstract class Geometry  {
    
 
   static public    ObservableInt grid_width = new ObservableInt(20);
   static public    ObservableInt n = new ObservableInt(1); // order;
   static public    ObservableInt lambda = new ObservableInt(632);
   static public    ObservableInt step_size = new ObservableInt(20);
   static public    double Pi_on_180 = Math.PI / 180;
   static public    String APP_NAME = "DiffCAD";
 static public void print(PrintStream out) {
        Print.output = out;
         print();
        Print.output = System.out;
   } // print
            
  static public void print() {
         laser.print();
        camera.print();
        wedge.print();
        grating.print();
        System.out.println("Lambda = "+ lambda.getValue());
        System.out.println("s = " + s());
   } // print
   

laser.java


import java.util.*;
import java.awt.*;
 
class Laser extends Shape{
    double W = 10;
    double H = 20;
 
 void print() {
        Print.class_name(this);
        print("Rotation= "+ rotation.getValue() + "\t");
        pc.println();
        // output to console, not file
        printNG();
   }
Print.print( ); - silly naming conventions
Print.classname( this ); OK
void print_class_name( ){
Geometry.output.print( this.getclass( ).getName( ) + "\t" );
}
static void class_name( Object o ){
Geometry.output.print( o.getclass( ).getName( ) + "\t" );
}

How to Read Files ?

try{
}catch{
Errors ;
}
if( arg.equals("Open") ) {
open( );
}
  static public void read_data_file(String file,double data[]) {
    System.out.println("processing:\t" + file);
    try {
        FileInputStream input_file = new 
                     FileInputStream(file);
                 StreamTokenizer tokens = new StreamTokenizer(input_file);
                 int next = 0;
                 int num = 0;
 
                 while ((next = tokens.nextToken()) != tokens.TT_EOF) 
                 {
                     switch (next) {
                        case tokens.TT_WORD: 
                            break;
                        case tokens.TT_NUMBER:
                            data[num] = (double) tokens.nval;
                            System.out.println(num+": "+ data[num]);
                            num = num + 1;     
                            break;
                        case tokens.TT_EOL:
                            break;
                     }
                 }
                 input_file.close();
        }
        catch (Exception exe) 
                {System.out.println("list_filtered_href_file:er!");}
    }
 
}

Objective : Read only Numbers, Ignore text

New Idea : tokenizer class does all the parsing

New class: StreamTokenizer

INPUT FILE ---> YOUR PROG ---->OUTPUTFILE

More Advanced Parsing : Filter HREF in HTML

Problem Statement :

Given a directory of files, form a list of those files with an HTML suffix.
Then process them into a new directory. Now the spaces in href's are
mapped to '%20'

Sample Quiz

1. FilenameFilter is a


 
homeContentsIndexPrevNext


 

UBLOGOLast Update: 04/09/97
Copyright © 1997- Douglas Lyon
Lyon@cse.bridgeport.edu