CS51 - Fall 2009 - Lecture 34

  • all of our i/o (input/output) classes reside in java.io
       - must import java.io.*

  • look at SantasListBasic code
       - try-catch IOException
          - when creating readers (i.e. opening the file), why?
             - file could not be there
             - file could not be readable (i.e. you don't have access to read it)
          - when reading data from a file
             - device failure (i.e. hard-drive fails or has problems)
             - corrupted file
       - System.err
       - Common setup when reading data
          //read one line
          while( line != null ){
             // do something with the line
             // read another line
          }
       - import java.io.*;

  • printing data out
       - similar class structure to reading data
       - Writers
          - FileWriter(String filename)
             - write() method
             - write is an overloaded method
          - PrintWriter(Writer writer)
             - printLn() // prints a line as well as an end of line
             - Again, allows us to use other things besides FileIO
          - Don't forget to call close(), otherwise it generally won't work right

  • For example, look at SantasListSlightlyModified code
       - readNames now throws an IOException
          - why did I do this?
       - printNames now takes a PrintWriter
       - but we can construct a PrintWriter out of System.out
       - functionality doesn't change!

  • However, makes it easier for us now to print this date to a file

  • We could copy and paste the results, but we'd like to write it using the program

  • How might we do this?

  • look at SantasList code
       - Create a new PrintWriter
       - need to catch the IOException again
          - nested try-catch blocks
       - nothing else changes from our previous version. This is the benefit of being able to pass different Writers to PrintWriter

  • look at SantasListApplication code
       - changed begin to main
       - notice that main is a static method (i.e. not associated with a particular object)
          - we need to create a new SantasListApplication object
          - then call the methods associated with that object
       - notice no window opens when we run it, it's an application, not an applet

  • Applets vs. applications
       - applets extend either "Applet" or "JApplet" class (or some class that extends from either of these)
       - program has to start somewhere
          - for our Applets, that extend WindowController, it's the begin() method
       - for applications:
          public static void main(String arguments[])

  • A simple I/O application
       // reads in first and last names
       // the first row in the returned array is an array of first names
       // the second row in the returend array is an array of last names
       // NUM_NAMES has the number of names
       String[][] readNames(String filename)

       // prints out all the names to filename with the characters reversed in each name
       void lastReversed(String filename, String[] last)

       // prints out all the names to filename with a space in between each character
       // e.g. "dave" -> "d a v e" (or "d a v e " if that makes life easier)
       void spacedFirstNames(String filename, String[] first)

       // prints out the names to a file in the form of "last, first"
       void reversedNames(String filename, String[] first, String[] last)