ITEC1011 Fall 2000

Assignment 6 Solution

import java.io.*;
import java.util.*; // fig 4-16

public class CopyFile1
{
 public static void main (String[] args) throws IOException
 {
  // TODO: Add initialization code here
  if( args.length != 2 ) // Chapter 6 fig 8
  {
   System.out.println( "Usage: CopyFile1 <source file> <dest file>" );
   return;
  }

  int alphaWordCount = 0;

  try
  {
   // see pg 4-38
   BufferedReader infile =
         new BufferedReader( new FileReader( args[0] ), 1 );

   // pg 4-39
   PrintWriter outfile =
     new PrintWriter(
                      new BufferedWriter(
                                         new FileWriter(
                                                         args[1]
                                                        )
                                         )
                    );
 
   String line;

   // see fig 4-16
   while( ( line = infile.readLine() ) != null )
   {
    // Print line to the output file
    outfile.println( line );

    // Count the number of "Alpha Words"
    StringTokenizer words = new StringTokenizer( line );
    while( words.hasMoreTokens() )
     if( alphaWord( words.nextToken() ) )
      ++alphaWordCount;
   }
 
   infile.close();
 
   // print the number of alpha words to the output file
   outfile.println( alphaWordCount );
   outfile.close();
  }
  catch( IOException e )
  {
   System.out.println( "IO Error" );
  }

 }

 private static boolean alphaWord( String s )
 {
  boolean isAlpha = true;
  for( int i = 0; isAlpha && i < s.length(); ++i )
   isAlpha = Character.isLetter( s.charAt(i) );
 
  return isAlpha;
 }

}