4.11 Week 11 -- Regular Expressions4 Operator Precedence and Associativity4.9 Week 9 - Collections  4.10 Week 10 -- I/O

4.10 Week 10 -- I/O

4.10.1 Reading and Web Sites

  1. Read van der Linden Chapter 13, Simple Input Output (pages 333-384)

4.10.2 Notes

  1. Character Streams
    1. Reader
    2. Writer
  2. Buffered - efficiency.
  3. Byte Streams
    1. InputStream
    2. OutputStream
  4. open, close
  5. Error trapping
    1. Files that don't exist.
    2. Files that can't be written to. (directories, no permission)
    3. Files that were never opened.
    4. What to do?
  6. public static void main(String [ ] args)
  7. Example: Cat2
    /* Louis Taber  PCC  4/21/2003  */
    /* File I/O demo program */
    
    import java.util.*;
    import java.io.*;
    
    
    class Cat
    {
    public static void main( String [] args)
      {
      boolean show_usage = false;
      BufferedReader Input =
          new BufferedReader( new InputStreamReader( System.in ) );
      PrintStream Output = System.out;
      PrintStream Error  = System.err;
    
    
      ARGS:
      for(int i=0; i<args.length; i++)
        {
        if( ! args[i].startsWith("-") ) continue;
        if( args[i].length() < 2 ) break;
        switch( args[i].charAt(1) )
          {
    
          case 'i':
            i++;      // move to next argument
            if( i >= args.length)
              {
              show_usage = true;
              break ARGS;
              }
            try
              {
              Input = new BufferedReader( 
                        new FileReader( args[i].trim() ) );
              }
            catch( FileNotFoundException e )
              {
              Error.println( "Input file not found:\n" + e );
              }
    
    
            System.out.println( "-i " + args[i]);
            continue ARGS;
    
    
          case 'o':   // Change output file
            i++;      // move to next argument
            if( i >= args.length)
              {
              show_usage = true;
              break ARGS;
              }
            try
              {
              Output = new PrintStream( 
                         new FileOutputStream( args[i].trim() ) );
              }
            catch( FileNotFoundException e )
              {
              Error.println( "Output file not found:\n" + e );
              }
    
          case 'l':   // Change error output to log file
            System.out.println( "-l ");
            i++;      // move to next argument
            if( i >= args.length)
              {
              show_usage = true;
              break ARGS;
              }
            try
              {
              Error = new PrintStream( 
                        new FileOutputStream( args[i].trim() ) );
              }
            catch( FileNotFoundException e )
              {
              Error.println( "Log file not found:\n" + e );
              }
            break;
    
          case  'h':  
          case  'H':
            System.out.println( "Help information: ");
    
          default:
            show_usage = true;
            continue ARGS;
          }  
    
        System.out.println( args[i]);
        }
    
      if( show_usage )
        {
        System.out.println( "Print usage ");
        System.exit(1);
        }
    
    
      Error.println( "An error ");
    
      String input_string; 
    
      try
        {  
        while( ( input_string = Input.readLine()) != null )
          Output.println( input_string );
        }
      catch( IOException e )
        {
        System.err.println( "File close() error:\n" + e );
        }  
    
      try{ Input.close(); }
      catch( IOException e )
        {
        System.err.println( "File close() error:\n" + e );    
        }
        Output.close();
        Error.close();
    
      System.exit(0);
      }
    }
    

4.10.3 Lab Assignment -- Sort

Write a sort routine. Well actually use a collection to do your sort, but do write a compareTo() method.

This program will also deal with:

Input a data file (or the standard input), sort the file by the specified keys, and write out the sorted data. Handle files of "all" lengths. From no records to over 1,000,000. By default, input the data as a String and do an ascending sort.

  1. Use a collection to store the data.
  2. Handle any number of records. (At least zero through one million.)
  3. If the command line has a "-i" option, use the following argument for the input file name. If there is no following argument, print out an error message and usage information.
  4. "-o" for the output file name. Otherwise use the standard output.
  5. "-h", print out the usage information.
  6. Start your program with just strings, add your own class later with a compareTo() method. (compareTo() is already written for String.)
  7. Use the following sort field argument: "-f" followed by a sort field specifier. The fields are comma separated. No spaces are allowed. Each field has a required start and stop column. These are hyphen separated and inclusive. There may be an optional "r" to reverse the sort, and an optional "n" to treat the field numerically.
  8. You may want to use a collection for the sort fields.
  9. You may want to use StringTokenizer(String str, String delim, boolean returnDelims) to break up the sort field argument.
  10. The ^ operator works on boolean arguments.
  11. If a "-F" is present, the sort information is in the following file name.
  12. Send an error report to the stderr file unless a "-l" option is specified, the send it to the specified log file. Log the line number of lines that have a numeric conversion error and discard those lines. You can do the error checking on input or in the compareTo() method.
  13. You may throw away duplicate entries.
  14. This lab is subject to modification if it proves to be too difficult.

Usage information:

java Sort [-i input]             // specify input file 
          [-o output]            // specify output file
          [-l log_file]          // specify log file
          [-f sort_fields]       // specify sort fields and order
          [-F sort_field_file]   // sort fields in: sort_field_file

  Sort the input file producing the output file.  If the -l option 
  is specified send errors to the log_file.  Use the sort fields 
  specified by -f or -F.

java Sort [-h]
  Print usage information

sort field specifier:
  [n][r]start_column-end_column[,...]
  n -- sort numerically.
  r -- reverse the order of the sort.
  fields are comma separated.
  No spaces are allowed.
  
Examples:

java Sort 
  sort the standard input ascending as strings sending the 
  output to the standard output.
java Sort -i abc
  Sort file abc
java Sort -f n50-60,r12-19
 Sort the file numerically on columns 50 - 60.  If their is a match, break the tie
 with a reverse alphabetic ordering of the field 12-19.

Sample data set:

a     45  b    -2   aaaaa     
b    3    b    -3   aaaab
c     4   b    -4   aaaax
d    -6   a    -2   aaaan
x    17   a    -3   aaaab
n     4   a    -4   aaaay

Sample sort fields:

-f 1-25   //  alpha sort all columns in order
-f 1-5    //  alpha sort first field
-f r1-5   //  alpha reverse sort first field
-f n6-10  //  numeric sort second field
-f nr6-10 //  numeric reverse sort second field
-f 21-25  //  alpha sort fifth field 
-f 11-15,1-5  
          // alpha sort third field, tie break on first
-f 11-15,r1-5 
          // alpha sort third field, reverse tie break on first
-f n16-19,n6-10  
          // numeric sort fourth field, tie break on second
-f n16-19,11-15
          // numeric sort fourth field, tie break on third

Please turn in a listing and an example program run. Test data will be provided.


Instructor: ltaber@pima.edu ** My new Home at GeoApps in Tucson ** The Pima College Site

4.11 Week 11 -- Regular Expressions4 Operator Precedence and Associativity4.9 Week 9 - Collections  4.10 Week 10 -- I/O