SDSU CS 535: Object-Oriented Programming & Design
Fall Semester, 1997
Doc 18 Java IO: File, StreamTokenizer

To Lecture Notes Index
San Diego State University -- This page last updated 22-Oct-97

Contents of Doc 18 Java IO: File, StreamTokenizer

  1. References
  2. File
    1. Creating Directories
    2. Creating Files
    3. Some File Data
    4. Machine Independence
    5. FixJavaDoc - An Example
  3. StreamTokenizer
    1. Terms and Defaults
      1. EOL Example
      2. Default Comment Character
      3. Quote Character
    2. Some Real Parsing

References



The Java Programming Language, Arnold and Gosling, Chapter 11

On-line Java Documentation

Java source code



Doc 18 Java IO: File, StreamTokenizer Slide # 1

File


java.io.File contains operations on files and directories
Constants

pathSeparator ( unix = : )
The system dependent path separator string.
pathSeparatorChar ( unix = : )
The system dependent path separator character.
separator ( unix = / )
The system dependent file separator String.
separatorChar ( unix = / )
The system dependent file separator character.
Constructors

File(String)
Creates a File object.
File(String, String)
Creates a File object from the specified directory.
File(File, String)
Creates a File object (given a directory File object).
File Methods
canRead()getParent() length()
canWrite()getPath() list()
delete()hashCode() list(FilenameFilter)
equals(Object)isAbsolute() mkdir()
exists()isDirectory() mkdirs()
getAbsolutePath()isFile() renameTo(File)
getName()lastModified() toString()


Doc 18 Java IO: File, StreamTokenizer Slide # 2
File Info

A file is specified by a path name, either
absolute path name
path name relative to the current working directory

The path name must follow the naming conventions of the host platform.


Doc 18 Java IO: File, StreamTokenizer Slide # 3

Creating Directories

import java.io.File;
import java.io.IOException;

class CreatingDirectories
   {
   public static void main( String args[] ) throws IOException
      {
      // One directory
      File courseDirectory = new File( "cs535" );
      courseDirectory.mkdir();
      
      // Subdirectory
      File gradesDirectory = new File( "cs535/grades" );
      gradesDirectory.mkdir();
      
      // Multiple directories
      // test and this must exist before this will work
      File  oneAtATime  =  new File(  "test/this/out"  );
      boolean createResult = oneAtATime.mkdir();

      if ( createResult )
         System.out.println( "Created test/this/out");
      else
         System.out.println( "Failed to create test/this/out");

      // creates all four directories at once
      File  severalAtOnce  =  new File( "second/test/of/this"  );
      
      if ( !severalAtOnce.exists() )
         createResult = severalAtOnce.mkdirs();
      }
   }
Output
Failed to create test/this/out
Created second/test/of/this

Doc 18 Java IO: File, StreamTokenizer Slide # 4

Creating Files

import java.io.*;

class Test
   {
   public static void main( String args[] ) throws IOException
      {
      // Open/Create file for writing

      FileWriter writeableFile = new FileWriter( "RightHere" );
      
      // Open file for reading, IOException if does not exists

      FileReader info = new FileReader( "ReadMe");
      
      // Safer Way

      File readMe = new File( "ReadeMe" );
      FileReader moreInfo;
      if ( readMe.exists() )
         moreInfo = new FileReader( readMe );
      
      // Open/Create file for appending

      boolean append = true;
      FileWriter exam = new FileWriter( "exam", append );
      PrintWriter cout = new PrintWriter( 
            new BufferedWriter( exam ));
      
      cout.println( "Question One: Write a class to...");
      cout.close();
      }
   }

Doc 18 Java IO: File, StreamTokenizer Slide # 5

Some File Data

import java.io.*;
import java.util.*
class FileInfo
   {
   public static void main( String args[] ) throws IOException
      {
      File exam = new File( "Exam" );

      System.out.println( "Absolute Path: " + 
                              exam.getAbsolutePath()  );

      System.out.println( "Path: " + exam.getPath()  );

      System.out.println( "Name: " + exam.getName()  );

      System.out.println( "Parent: " + exam.getParent()  );

      System.out.println( "Can read: " + exam.canRead()  );

      System.out.println( "Can write: " + exam.canWrite()  );

      System.out.println( "Length: " + exam.length()  );

      System.out.println( "Modified?: " + exam.lastModified()  );
      System.out.println( "Modified!: " + 
                              new Date(exam.lastModified())  );
      }
   }
Output
Absolute Path: /Programming/JavaPrograms/classNotes/Exam
Path: Exam
Name: Exam
Parent: null
Can read: true
Can write: true
Length: 710
Modified?: 877380994000
Modified!: Mon Oct 20 12:56:34 ADT 1997

Doc 18 Java IO: File, StreamTokenizer Slide # 6

Machine Independence


Be careful about embedding Unix (Dos, Mac) specific files names in programs

import java.io.*;
class  MachineIndependentFileNames
   {
   public  static  void  main( String  args[]  )
      {
      String  macFile  =  ":home:ma:whitney:cs596:grades";

      String currentPlatformFile
           =  gradeFile.replace( ':', File.separatorChar );

      }
   }

Doc 18 Java IO: File, StreamTokenizer Slide # 7

FixJavaDoc - An Example


JavaDoc produces HTML documentation from Java source code

However it assumes that some items ( images, standard docs) are in a fixed position

Will write a program to read all .html files in a directory and modify them to properly access images etc. using local resources

Doc 18 Java IO: File, StreamTokenizer Slide # 8
Listing all files in a Directory
import java.io.*;

class ListAll
   {
   public static void main( String args[] ) throws IOException
      {
      File currentDirectory = new File( "." );
      String[] listing = currentDirectory.list();
      
      for ( int k = 0; k < listing.length; k++ )
         System.out.println( listing[k] );
      }
   }
Output
Property
randomNumbers.html
randomNumbers.java
readableSerialize.html
readableSerialize.java
ReadingFileExample.java

Doc 18 Java IO: File, StreamTokenizer Slide # 9
Listing all .html files in a Directory
import java.io.*;

class TheNoReuseWay
   {
   public static void main( String args[] ) throws IOException
      {
      File currentDirectory = new File( "." );
      String[] listing = currentDirectory.list();
      
      for ( int k = 0; k < listing.length; k++ )
         if ( listing[k].endsWith( ".html" ))
            System.out.println( listing[k] );
      }
   }
Output
randomNumbers.html
readableSerialize.html

Doc 18 Java IO: File, StreamTokenizer Slide # 10
Listing all .html files in a Directory With Reuse
import java.io.*;

class Test
   {
   public static void main( String args[] ) throws IOException
      {
      File currentDirectory = new File( "." );
      String[] listing = currentDirectory.list( 
            new FileExtensionFilter("html"));
      
      for ( int k = 0; k < listing.length; k++ )
         System.out.println( listing[k] );
      }
   }

public class FileExtensionFilter implements FilenameFilter
   {
   String fileExtension;
   
   public FileExtensionFilter( String extension )
      {
      if ( extension.startsWith( "." ))
         fileExtension = extension;
      else
         fileExtension = "." + extension;
      }

    public  boolean accept(File dir, String name)
      {
      if ( name.endsWith( fileExtension ) )
         return true;
      else
         return false;
      }
   }


Doc 18 Java IO: File, StreamTokenizer Slide # 11
Processing the File
   static final String imageURL = 
      "src=\"http://www.eli.sdsu.edu/doc/images";
   static final String imageReference = "src=\"images";
   static Random randomNumber = new Random();
   
   public static void correctImageReference( String fileName) 
         throws Exception
      {
      File originalFile = new File( fileName );
      File scratchFile =  scratchFile();
      PrintWriter cout = null;
      StringReplaceReader fixed = null;
         
      BufferedReader bufferedFile = 
         new BufferedReader( new FileReader( originalFile ) );
      
      fixed = new StringReplaceReader( 
               bufferedFile, imageReference, imageURL);
         
      cout = new PrintWriter
         ( new BufferedWriter( new FileWriter( scratchFile )));
      
      cout.print( fixed.contents() );
         
      cout.close();
      fixed.close();
         
      originalFile.delete();
      scratchFile.renameTo( originalFile);
      }

Doc 18 Java IO: File, StreamTokenizer Slide # 12
Finding a Unique name for a Temporary file

   /**
    * Return a file which has a name
    * that does not exist in the current directory
    */
   public static File scratchFile()
      {

      String newFileName;
      File newFile;
      do
         {
         newFileName = "" + randomNumber.nextInt();
         newFile = new File( newFileName);
         }
      while ( newFile.exists() );

      return newFile;
      }

Doc 18 Java IO: File, StreamTokenizer Slide # 13
The Listing for Now
/**
 * This class fixes a problem with javadoc. Javadoc assumes that
 * all images are in a subdirectory of your current directory.
 * This class reads all files in the current directory that 
 * end in ".html". All references to standard java documentation
 * images in these files are modified to get the images over 
 * the network from www.eli.sdsu.edu.
 *
 * @author Peter Proud-Madruga, Roger Whitney
 * @version 0.8 20 October 1997
 */
class FixJavaDoc
   {
   static final String imageURL = "src=\"http://www.eli.sdsu.edu/doc/images";
   static final String imageReference = "src=\"images";
   static Random randomNumber = new Random();
   
   public static void main( String args[] ) throws Exception
      {
      File currentDirectory = new File( "." );
      String[] listing = currentDirectory.list( new FileExtensionFilter("html"));
      
      for ( int k = 0; k < listing.length; k++ )
         {
         System.out.println( "Processing file: " + listing[k]);
         correctImageReference( listing[k] );
         }
      }
   /**
    * Replaces all occurances of the string imageReference with 
    * the string imageURL in the file "fileName"
    */
   public static void correctImageReference( String fileName)
      {
      File originalFile = new File( fileName );
      File scratchFile =  scratchFile();
      PrintWriter cout = null;
      StringReplaceReader fixed = null;
      try
         {
         BufferedReader bufferedFile = 
            new BufferedReader( new FileReader( originalFile ) );
         
         //   StringReplaceReader will replace all occurrances
         // of imageReference with imageURL
         fixed = new StringReplaceReader( 
                  bufferedFile, imageReference, imageURL);
         
         cout = new PrintWriter
            ( new BufferedWriter( new FileWriter( scratchFile )));
         
         // fixed.contents returns file contents after making the 
         // changes
         cout.print( fixed.contents() );
         
         // can't delete or rename file that has an open stream 
         cout.close();
         fixed.close();
         
         originalFile.delete();
         scratchFile.renameTo( originalFile);
         }
         
      // only files that exist are opened for reading
      catch (FileNotFoundException shouldNotHappen)
         {
         System.err.println( "FileNotFoundException on existing file");
         shouldNotHappen.printStackTrace();
         }
         
      catch (IOException ioError )
         {
         System.err.println( "IO error while processing file: " + 
               fileName);
         ioError.printStackTrace();
         
         cout.close();
         scratchFile.delete();
         }
      }
   
   /**
    * Return a file which has a name
    * that does not exist in the current directory
    */
   public static File scratchFile()
      {
      String newFileName;
      File newFile;
      do
         {
         newFileName = "" + randomNumber.nextInt();
         newFile = new File( newFileName);
         }
      while ( newFile.exists() );
      return newFile;
      }
   }

Doc 18 Java IO: File, StreamTokenizer Slide # 14

StreamTokenizer

Methods
commentChar(int) pushBack()
eolIsSignificant(boolean) quoteChar(int)
lineno() resetSyntax()
lowerCaseMode(boolean) slashSlashComments(boolean)
nextToken() slashStarComments(boolean)
ordinaryChar(int) toString()
ordinaryChars(int, int) whitespaceChars(int, int)
parseNumbers() wordChars(int, int)


StreamTokenizer
More complex than the StringTokenizer
Designed to parse "Java-style" input
Parses numbers
Parses Java-style comments


Doc 18 Java IO: File, StreamTokenizer Slide # 15
nextToken

Returns the type of the next token

Places token type in field ttype

Places the token in a public field
sval for string (or word )
nval for number (declared a double not Double)
Token Types

TT_WORD
A string (word) was scanned
The string field sval contains the word scanned
TT_NUMBER
A number was scanned
Only decimal floating-point numbers (with or without a decimal point) are recognized
TT_EOL
End-of-line scanned
TT_EOF
End-of-file scanned


Doc 18 Java IO: File, StreamTokenizer Slide # 16
Simple Example
import java.io.*;

class  Tokenizer  
   {
   public static void main( String args[] ) throws Exception 
      {
      String sample = "this 123 is an 3.14 \n simple test";
      InputStream in;
      in = new StringBufferInputStream( sample );
      StreamTokenizer parser = new StreamTokenizer( in );
      
      while ( parser.nextToken() != StreamTokenizer.TT_EOF )
         {
         if ( parser.ttype == StreamTokenizer.TT_WORD)
            System.out.println( "A word: " + parser.sval );
         else if  ( parser.ttype == StreamTokenizer.TT_NUMBER )
            System.out.println( "A number: " + parser.nval );
         else if  ( parser.ttype == StreamTokenizer.TT_EOL )
            System.out.println( "EOL" );
         else
            System.out.println( "Other: " + (char) parser.ttype );
         }   
      }
   }
Output
A word: this
A number: 123
A word: is
A word: an
A number: 3.14
A word: simple
A word: test


Doc 18 Java IO: File, StreamTokenizer Slide # 17

Terms and Defaults

Special Characters
Characters that are treated special
White space
Characters that make up numbers
Characters that make up words (tokens)

Ordinary Characters
All nonspecial characters
Default Values

Word characters
a - z
A - Z
ascii values 130 through 255

White space
ascii values 0 through 32
includes space, tab, line feed, carriage return, escape

comment character /
quote characters " (double quote) ' (single quote )
parse numbers on
Default to OFF
eolIsSignificant are ends of line significant
lowerCaseMode converts all to lower case
slashSlashComments recognize /* */ comments
slashStarComments recognize // comments

Doc 18 Java IO: File, StreamTokenizer Slide # 18
Warning
Documentation states:
  public void resetSyntax()

       Resets the syntax table so that all characters are special. 


Source code states:
  public void resetSyntax()

       Resets the syntax table so that all characters are special. 

Source code does:
  public void resetSyntax()

       Resets the syntax table so that all characters are ordinary. 


Doc 18 Java IO: File, StreamTokenizer Slide # 19

EOL Example

import java.io.*;

class  Tokenizer  
   {
   public static void main( String args[] ) throws Exception 
      { 
      String sample = "this 123 is an 3.14 \n simple test";
      InputStream in =  new StringBufferInputStream( sample );
      StreamTokenizer parser = new StreamTokenizer( in );
      parser.eolIsSignificant( true ); 
      
      while ( parser.nextToken() != StreamTokenizer.TT_EOF )
         {
         if ( parser.ttype == StreamTokenizer.TT_WORD)
            System.out.println( "A word: " + parser.sval );
         else if  ( parser.ttype == StreamTokenizer.TT_NUMBER )
            System.out.println( "A number: " + parser.nval );
         else if  ( parser.ttype == StreamTokenizer.TT_EOL )
            System.out.println( "EOL" );
         else
            System.out.println( "Other: " + (char) parser.ttype );
         }   
      }
   }
Output
A word: this
A number: 123
A word: is
A word: an
A number: 3.14
EOL
A word: simple
A word: test

Doc 18 Java IO: File, StreamTokenizer Slide # 20

Default Comment Character

import java.io.*;

class  Tokenizer  
   {
   public static void main( String args[] ) throws Exception 
      { 
      String sample = "this 123 /is an 3.14 \n simple test";

      InputStream in = new StringBufferInputStream( sample );
      StreamTokenizer parser = new StreamTokenizer( in );
      
      while ( parser.nextToken() != StreamTokenizer.TT_EOF )
         {
         if ( parser.ttype == StreamTokenizer.TT_WORD)
            System.out.println( "A word: " + parser.sval );
         else if  ( parser.ttype == StreamTokenizer.TT_NUMBER )
            System.out.println( "A number: " + parser.nval );
         else if  ( parser.ttype == StreamTokenizer.TT_EOL )
            System.out.println( "EOL" );
         else
            System.out.println( "Other: " + (char) parser.ttype );
         }   
      }
   }
Output
A word: this
A number: 123
A word: simple
A word: test

Doc 18 Java IO: File, StreamTokenizer Slide # 21

Quote Character

import java.io.*;
class  Tokenizer  
   {
   public static void main( String args[] ) throws Exception 
      { 
      String sample = "this #123 /is an 3.14# \n simple test";

      InputStream in = new StringBufferInputStream( sample );
      StreamTokenizer parser = new StreamTokenizer( in );

      parser.quoteChar( '#' );

      while ( parser.nextToken() != StreamTokenizer.TT_EOF )
         {
         if ( parser.ttype == StreamTokenizer.TT_WORD)
            System.out.println( "A word: " + parser.sval );
         else if  ( parser.ttype == StreamTokenizer.TT_NUMBER )
            System.out.println( "A number: " + parser.nval );
         else if  ( parser.ttype == StreamTokenizer.TT_EOL )
            System.out.println( "EOL" );
         else
            System.out.println( "Other: " + (char) parser.ttype +
                           "sval: " + parser.sval );
         }   
      }
   }
Output
A word: this
Other: #sval: 123 /is an 3.14
A word: simple
A word: test

Doc 18 Java IO: File, StreamTokenizer Slide # 22
\n always counts between Quote Characters
import java.io.*;
class  Tokenizer  
   {
   public static void main( String args[] ) throws Exception 
      { 
      String sample = "this #123 /is an 3.14 \n simple # test";

      InputStream in = new StringBufferInputStream( sample );
      StreamTokenizer parser = new StreamTokenizer( in );

      parser.quoteChar( '#' );

      while ( parser.nextToken() != StreamTokenizer.TT_EOF )
         {
         if ( parser.ttype == StreamTokenizer.TT_WORD)
            System.out.println( "A word: " + parser.sval );
         else if  ( parser.ttype == StreamTokenizer.TT_NUMBER )
            System.out.println( "A number: " + parser.nval );
         else if  ( parser.ttype == StreamTokenizer.TT_EOL )
            System.out.println( "EOL" );
         else
            System.out.println( "Other: " + (char) parser.ttype +
                           "sval: " + parser.sval );
         }   
      }
   }
Output
A word: this
Other: #sval: 123 /is an 3.14
A word: simple
Other: #sval: test

Doc 18 Java IO: File, StreamTokenizer Slide # 23
Some Parsing Fun
import java.io.*;
class  Tokenizer  {
   public static void main( String args[] ) throws Exception { 

      String sample = "this #123 /is an 3.14 \n simple # test";

      InputStream in = new StringBufferInputStream( sample );
      StreamTokenizer parser = new StreamTokenizer( in );

      parser.wordChars( 0, 255 );
      parser.ordinaryChar( 's' );

      while ( parser.nextToken() != StreamTokenizer.TT_EOF )
         {
         if ( parser.ttype == StreamTokenizer.TT_WORD)
            System.out.println( "A word: " + parser.sval );
         else if  ( parser.ttype == StreamTokenizer.TT_NUMBER )
            System.out.println( "A number: " + parser.nval );
         else if  ( parser.ttype == StreamTokenizer.TT_EOL )
            System.out.println( "EOL" );
         else
            System.out.println( "Other: " + (char) parser.ttype +
                           "sval: " + parser.sval );
         }   
      }
   }
Output
A word: thi
Other: ssval: null
A word: #123 /i
Other: ssval: null
A word: an 3.14 
 
Other: ssval: null
A word: imple # te
Other: ssval: null
A word: t

Doc 18 Java IO: File, StreamTokenizer Slide # 24

Some Real Parsing

class  Tokenizer  
   {
   public static void main( String args[] ) throws Exception 
      { 

      String sample = "name=value; sam = \n A";

      InputStream in = new StringBufferInputStream( sample );
      StreamTokenizer parser = new StreamTokenizer( in );

      parser.wordChars( 0, 255 );
      parser.whitespaceChars( 0, 32 );
      parser.ordinaryChar( ';' );
      parser.ordinaryChar( '=' );

      while ( parser.nextToken() != StreamTokenizer.TT_EOF )
         {
         if ( parser.ttype == StreamTokenizer.TT_WORD)

         // same as previous examples
         }   
      }
   }
Output
A word: name
Other: =sval: null
A word: value
Other: ;sval: null
A word: sam
Other: =sval: null
A word: A

Doc 18 Java IO: File, StreamTokenizer Slide # 25
Getting Fancy
class  Tokenizer  
   {
   public static void main( String args[] ) throws Exception 
      { 
      String sample = "name with space=value # a commment\n; "
                     + " 'sam =cat' = \n A";

      InputStream in = new StringBufferInputStream( sample );
      StreamTokenizer parser = new StreamTokenizer( in );

      parser.wordChars( 0, 255 );
      parser.whitespaceChars( 9, 13 ); //tab, line feed, return
      parser.ordinaryChar( ';' );
      parser.ordinaryChar( '=' );
      parser.commentChar( '#' );
      parser.quoteChar( '\'' );

      while ( parser.nextToken() != StreamTokenizer.TT_EOF )
         {
         // Same as other examples
         }   
      }
   }
Output
A word: name with space
Other: =sval: null
A word: value
Other: ;sval: null
Other: 'sval: sam =cat
Other: =sval: null
A word: A

Doc 18 Java IO: File, StreamTokenizer Slide # 26
Toggling
   public static void main( String args[] ) throws Exception 
      { 
      String sample = "name;with;space=value # a commment\n; "
                     + "sam & cat = \n A";
      InputStream in = new StringBufferInputStream( sample );
      StreamTokenizer parser = new StreamTokenizer( in );

      parser.wordChars( 0, 255 );
      parser.whitespaceChars( 9, 13 ); //tab, line feed, return
      parser.ordinaryChar( '=' );
      parser.commentChar( '#' );

      while ( parser.nextToken() != StreamTokenizer.TT_EOF )
         {
         if ( parser.ttype == StreamTokenizer.TT_WORD)
            System.out.println( "A word: " + parser.sval );
         else if  ( parser.ttype == '=' )
            {
            System.out.println( "=: ");
            parser.ordinaryChar( ';' );
            parser.wordChars( '=', '=' );
            }
         else if  ( parser.ttype == ';' )
            {
            System.out.println( ";: " );
            parser.ordinaryChar( '=' );
            parser.wordChars( ';', ';' );
            }
         else
            System.out.println( "Error: " + (char) parser.ttype +
                           "sval: " + parser.sval );
         }   
      }

Doc 18 Java IO: File, StreamTokenizer Slide # 27
Output
A word: name;with;space
=:
A word: value
;:
A word: sam & cat
=:
A word: A



visitors since 22-Oct-97