Lecture 34, CS 302-7 and 8, April 16

  1. Various things
    1. This week:

                                                               i.      Hand back tests

    1. Thoughts on exam 2
    2. Program 3

                                                               i.      Consulting hours

                                                             ii.      Questions?

  1. Reading/writing text files
    1. Why?  Everything on a computer, pretty much, is a text file (even if it represents some other format).

                                                               i.      Documents, html, .java, .class, pictures, etc…

    1. How?  Scanner made from a file object
    2. java.io package
  1. File
    1. File is a class.  A file object represents the contents of a text file.
    2. File <varName>=new File(<txtFileName>);
  2. Where does file name come from?
    1. We can hard code it
    2. But, this isn’t great – it means we have to redo our code every time we want to read a different file
    3. A better solution is to use either of the following:

                                                               i.      command line arguments

                                                             ii.      scanner input

  1. Input
    1. Scanner of file
    2. Scanner Review:

                                                               i.      new Scanner(System.in);

                                                             ii.      new Scanner(string);

    1. We can also do

                                                               i.      new Scanner(file)

    1. So, Scanner in = new Scanner(<txtFileName>);
    2. We can treat this just the same as a Scanner of a string or System.in, using the same methods

                                                               i.      hasNext(), next(), nextLine(), etc

    1. Note –

new Scanner(<txtFileName>); does not work

                                                               i.      You can do this on one line with:

new Scanner(new File(<txtFileName>));

  1. Output
    1. So, if we want to read input from a file, we can use the above
    2. But, what if we want to write to a file?
    3. PrintWriter class

                                                               i.      An object of PrintWriter represents text that will be written to a file

    1. Construct:

PrintWriter <varName> = new PrintWriter(<txtFileName>);

PrintWriter out = new PrintWriter(“output.txt”);

    1. How the text file works here:

                                                               i.      If it doesn’t exist, it is created in this directory.

                                                             ii.      If it already exists, we empty it before writing

    1. Methods that we can use for writing to a file:

                                                               i.      print, println, printf

                                                             ii.      If these look familiar – they should.

1.      System.out is actually an object of a class called PrintStream.  PrintWriter is similar to this (an enhancement of it).

2.      Techincally: System is a class name, out is a public static variable storing an object of the PrintStream class

    1. Always close Scanner and PrintWriter when you are done processing a file:

                                                               i.      .close()

                                                             ii.      If we don’t do this, might not write everything to the file…

                                                            iii.      Due to how write buffers work

  1. Exceptions
    1. Write program – ReadAndWrite.java

                                                               i.      Java.io package

    1. Problem – won’t compile.  Why?
    2. Exceptions

                                                               i.      We’ve seen these before:

1.      Null Pointer Exception

2.      IndexOutOfBounds Exception

                                                             ii.      These are run-time exceptions.

                                                            iii.      In general, we can’t really ‘predict’ when they might happen.  If we make our code carefully, we can prevent them, though.

                                                           iv.      But, there are some exceptions that are ‘out of our hands’, so to speak.

                                                             v.      For instance, imagine if you’re trying to read from an input file, but the file does not exist on your computer.

                                                           vi.      Your program doesn’t check to see if it exists – it just assumes it does.  But it’s possible that it doesn’t.  Since this is a ‘predictable’ situation, Java forces us to handle it elegantly.

                                                          vii.      In general: There are some things that we can do in Java that will force Java us to do something special to prevent a run-time error.

                                                        viii.      We must ‘handle’ the exception that can be thrown by a statement.

                                                           ix.      Unchecked vs checked exceptions

    1. There are two ways to handle errors.

                                                               i.      Throws and try/catch

1.      We’ll do try/catch next week

                                                             ii.      Throws today

  1. throws keyword
    1. How to use?

<method declaration> throws FileNotFoundException

    1. What does this mean?

                                                               i.      We’re basically telling Java that we know that this might not work, but it should go ahead and do it anyway.

                                                             ii.      Note – this doesn’t prevent errors at all.  If the file doesn’t exist, we’ll still error out

1.      It just informs Java that we’re willing to accept that consequence

                                                            iii.      Note – you can only declare this at the method level.  Doesn’t work at class level, line level, etc.

    1. Working Example – ReadAndWrite.java