i. There will be class Wednesday
1. Hand back tests (probably)
ii. There will be lab tomorrow
i. Documents, html, .java, .class, pictures,
etc…
File <varName>=new File(<txtFileName>);
i. new Scanner(System.in);
ii. new Scanner(string);
i. new Scanner(file)
Scanner < varName > = new Scanner(<file>);
i. hasNext(), next(), nextLine(), etc
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>));
i. An object of PrintWriter represents text that
will be written to a file
PrintWriter <varName> = new PrintWriter(<txtFileName>);
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
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
i. Null Pointer Exception
ii. IndexOutOfBounds Exception
i. Usually, if we make our code carefully, we
can prevent them
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.
i. Try/catch
1. We’ll do this next week
ii. throws
<method declaration> throws FileNotFoundException
eg:
public static void main(String[] args) throws FileNotFoundException
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.
i. Note – on Linux/Mac machines, you don’t need
to worry about this…
i. Escape characters – eg \n for newline.
ii. \\ is similar, except it means “Single \”
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
i. In java.net package
ii. Object represents the address of a webpage
iii. Scanner constructed on url.openStream() [which
returns an inputStream object]