i.
java.io.File;
ii.
java.util.Scanner;
1.
new
Scanner(File f);
i.
java.io.PrintWriter;
i.
throws
ii.
Java.io.FileNotFoundException;
i.
Say you’re
trying to read a file from some location on your computer other than the
current directory.
ii.
You’ll need to
specify a full file name.
iii.
On Windows
machines, these include the \ character.
1.
Note – on
Linux/Mac machines, you don’t need to worry about this…
iv.
Why might this
be a problem?
1.
Escape
characters – eg \n for newline.
2.
\\ is similar,
except it means “Single \”
v.
So,
“c:\\directory\\file.txt”
vi.
You probably
won’t have to do this very often, though
vii.
Note – this
only applies in your code, not in what you supply to the program at run-time
(eg user input)
viii.
Note – what if
you’re not sure your program will be run on Windows or Mac
1.
File.separator
2.
This is a
static String in the File class
3.
Represents the
correct separator character (\\ or /) for your system
i.
In java.net
package
ii.
Object
represents the address of a webpage
iii.
Scanner based
on url.openStream() [which returns an inputStream object]
i.
Apply to cs302
website
i.
Default
delimiter - whitespace
ii.
The book’s
discussion about options for the parameter here is not very satisfying
1.
In general it
accepts something called regular expressions
a.
These are like
patterns
b.
We won’t worry
about the details in this class
2.
But, you can
also provide a specific string that you want Java to use as a delimiter for the
items in the Scanner
iii.
Simple example…
1.
Specify String
for delimiter, such as “,” or “:”
a.
With delimiter
“,”, the following: “Hello,goodbye” is parsed into “Hello” and “goodbye”
2.
This overrides
the default – whitespace will no longer separate parts of the input if you
specify a delimiter
3.
Can be more
than one character long, such as “…“ or “, “
a.
If you use more
than one character, it must match the string exactly
b. So, with the delimiter “…”, the following will be returned as one String: “a, b. c d..e”
4. useDelimiter(“”) – read one character at a time