CS51 - Spring 2010 - Lecture 22

  • Exercise 18.9.3

  • Admin
       - NO class on Wed. (happy thanksgiving!)
       - TP2 designs due Wed at 11 (e-mail or drop them off in person)
       - no TA office hours over the break (or on Monday 11/29)

  • Quick review of exceptions
       - what is an exception/why are they useful?
       - exception hierarchy
       - checked vs. unchecked exceptions
       - throws keywords
       - throw keyword

  • Why do we need files?
       - persist data
       - when a program exits, any variables, data, etc. is lost unless we store it to a file

  • Basic computer hardware
       - processor
       - main memory
       - hard-drive

  • show SantasListBasic demo
       - similar to BadName
       - now we don't have to enter the names
       - we can read them from a file

  • What operations might you do on a file?
       - open
       - read
       - write
       - close/save

  • Reading data from files
       - Parent class is "Reader"
       - Two main Readers
          - FileReader(String filename)
             - read() // reads one character at a time
             - extends Reader (so is of type Reader)
          - BufferedReader(Reader reader)
             - readLine();
                - returns the next whole line as a String
                - if no more data is available (end of file) returns null
             - notice it takes as input another reader, why?
                - we'll see later, can be used for more than just reading files
                - could pass in another type of reader
                   - for example, System.in, so we can read a line at a time from the console
             - call close() when you're done
             - is also of type "Reader"
          - Both of these classes are in java.io
             - we need to import these classes at the top, like we do with objectdraw:
                import java.io.*;

  • look at names file

  • BufferedReader reader = new BufferedReader(new FileReader(String filename))
       - how can we read all of the data from the names file using a BufferedReader?

  • 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
       - 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
             - look at java.io.FileWriter online
          - 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
          - separate 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[])