Lecture 34, CS 302-6, November 21

  1. Various things
    1. This week:

                                                               i.     There will be class Wednesday

1.    Hand back tests (probably)

                                                             ii.     There will be lab tomorrow

  1. Review:
    1. Steganography
  2. 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. File
    1. File is a class.  A file object represents the contents of a text file.

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

  1. Input
    1. We’ll read from a File using a Scanner based on that file
    2. Scanner Review:

                                                               i.     new Scanner(System.in);

                                                             ii.     new Scanner(string);

    1. We can also do

                                                               i.     new Scanner(file)

Scanner < varName > = new Scanner(<file>);

    1. 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 (since <txtFileName> is a string)

                                                               i.     You can declare/construct on one line with:

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

  1. Output
    1. What if we want to write to a file?
    2. PrintWriter class

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

    1. Construct:

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

    1. How files work in this case:

                                                               i.     If the file doesn’t already exist, it is created in the code directory.

                                                             ii.     If it already exists in this directory, we empty it before writing to it

    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.    Behind the scenes: System is a class name, out is a public static variable storing an object of the PrintStream class

  1. Reminder:
    1. You must always close Scanner and PrintWriter when you are done processing a file:
    2. scan.close(), printWriter.close();
    3. If you don’t do this, you might not write everything to the file…
  2. Exceptions
    1. We’ve seen these before:

                                                               i.     Null Pointer Exception

                                                             ii.     IndexOutOfBounds Exception

    1. Exceptions occur at run-time, when something goes wrong in your program.
    2. In general, we can’t really ‘predict’ when they might happen (if we could, we’d throw them at compile time).

                                                               i.     Usually, if we make our code carefully, we can prevent them

    1. There are some exceptions that are ‘out of our hands’, so to speak.

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

                                                             ii.     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.

                                                            iii.     We must ‘handle’ the possible exception.

    1. There are two ways to handle exceptions.

                                                               i.     Try/catch

1.    We’ll do this next week

                                                             ii.     throws

  1. throws keyword
    1. How to use?

<method declaration> throws FileNotFoundException

eg:

public static void main(String[] args) 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 possible run-time errors at all.  If the file doesn’t exist, we’ll still error out

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

    1. ReadAndWrite.java
  1. Absolute and relative file names
    1. Relative – current directory
    2. Absolute – specify whole location/file name
    3. On Windows machines, these include the \ character.

                                                               i.     Note – on Linux/Mac machines, you don’t need to worry about this…

    1. Why might this be a problem?

                                                               i.     Escape characters – eg \n for newline.

                                                             ii.     \\ is similar, except it means “Single \”

    1. So, we can represent a file path on Windows like: “c:\\directory\\file.txt”
    2. Note – this only applies in your code, not in what you supply to the program at run-time (eg user input)
    3. Note – what if you’re not sure your program will be run on Windows or Mac?

                                                               i.     File.separator

                                                             ii.     This is a static String in the File class

                                                            iii.     Represents the correct separator character (\\ or /) for your system

1.    So, you can write the same program for either platform

  1. Reading websites
    1. You can use a Scanner to read a webpage
    2. URL class

                                                               i.     In java.net package

                                                             ii.     Object represents the address of a webpage

                                                            iii.     Scanner constructed on url.openStream() [which returns an inputStream object]

    1. This throws an IOException
    2. HTMLReader.java
  1. Homework – For Wednesday – Program 3