File I/O
April 22nd, 2016
Lecture Code
Reading from Files
- Scanner in = new Scanner (new File ("usher.in"));
- Note: opening a File can generate a FileNotFoundException.
- Note: use in.hasNextLine() is one way to determine whether the end of file
has been reached.
- in.close();
Writing to Files
- PrintWriter out = new PrintWriter (new File ("usher.out"));
- Note: opening a File can generate a FileNotFoundException.
- out.close();
Class Exercise
Write a program to count the number of times that each letter
of the alphabet appears in the text of Edgar Allen Poe's
story The Fall of the House of Usher.
The text appears in a file named usher.in.
Character frequency information should be written to a file
named usher.out.
Additional examples: