Today: File I/O patterns
Wednesday, May 13: search/sort recap, the File-as-path mental model, the next* cascade, count-allocate-fill, and the Lab 11 launch
In a nutshell
Today is the hinge of Week 7. Monday and Tuesday set up the story (read a file into an array, do array work, write the result) and introduced new Scanner(new File(path)). Today we add the second half of that arc: choosing the right read method when the file has mixed types, walking the file in two passes to size and fill a fixed array, and writing a result back out with PrintStream. By the end of class you have everything you need to start Lab 11.
One-sentence summary. A
Fileis a path, not the contents; aScannerdoes the reading, aPrintStreamdoes the writing, andcount-allocate-fillis how we turn a file of unknown length into a fixed-size array.
By the end of today you will be able to:
- Order the
hasNext*cascade correctly (int before double before general token) and explain why. - Recognize the leftover-newline trap on sight and apply the throwaway
sc.nextLine()fix. - Construct a file
PrintStreamwithnew PrintStream(new File(path))and explain whatclose()does that nothing else does. - Walk a file twice (count, then allocate, then fill) to load an unknown-length file into a fixed array.
- Read the Lab 11 file format and identify which
next*method each column needs.
The class flow
1. Recap: search and sort (Thursday's quiz)
2. New today: File is a path (one new thing)
3. Four-move File lifecycle (null/blank check, exists check, wrap, close)
4. The hasNext cascade table
5. Count-allocate-fill: the two-pass pattern
6. Lab 11 file format
7. countLines, in English then in code
8. main: the orchestration scaffold
9. The three fillArray overloads
10. PrintStream: writing a result
11. Lab 11 launch
Today’s objectives (Day 3)
These map to objectives O3.0 through O3.6 in the week’s contract.
| ID | Objective |
|---|---|
| O3.0 | Given a file with a known token shape, choose between nextLine, nextInt, nextDouble, and next. |
| O3.1 | State the cascade order rule: hasNextInt before hasNextDouble. Trace a small mixed-type file and produce the line-by-line classification. |
| O3.2 | Recognize the leftover-newline trap. Apply the throwaway sc.nextLine(); fix at the boundary. |
| O3.3 | Construct a file PrintStream with new PrintStream(new File(path)). Use print, println, printf the same way as on the console. |
| O3.4 | State the close-flushes-buffer rule. Predict the empty-or-truncated-output bug as a missing close(). |
| O3.5 | Write an end-to-end program that reads from one file and writes to another, with one throws FileNotFoundException on main. |
| O3.6 | Choose between the nextInt + throwaway-nextLine pattern and the nextLine + String.split(",") pattern for record-shaped input. |
Today’s lessons on the site
The slides cover the live class flow; the site lessons are the searchable, re-readable version of the same material. Read in this order before lab time tonight.
- 7c: The next* Cascade and the nextLine Trap (~18 min): choosing the right read method, the
hasNextIntbeforehasNextDoubleorder rule, and the leftover-newline trap. - 7d: Writing Files with PrintStream (~13 min):
System.outis already aPrintStream; pointing one at a file is the only new step; closing matters. - 7e: Count-Allocate-Fill (~18 min): the two-pass pattern for loading an unknown-length file into a fixed array. We touch this today to launch Lab 11; we will return to it Friday.
Lab 11 launches today
Lab 11 is the first lab where the input comes from a file. The spec uses every pattern we cover today: a File path argument, a Scanner-on-File loop, the next* cascade for mixed columns, count-allocate-fill to size the array, and a PrintStream for the output report. Read 7c, 7d, and 7e tonight, then start the lab.
This week at a glance
| Day | Topic | Site lessons | Slides |
|---|---|---|---|
| Mon 05-11 | Searching and sorting in a file context. The story arc. Just-enough Scanner-on-File. | 7a | mon-05-11-selection-insertion-sort.pdf |
| Tue 05-12 | File as path, Scanner-on-File, throws FileNotFoundException as the course route. |
7b | tue-05-12-binary-search-and-file-io.pdf |
| Wed 05-13 (today) | The next* cascade, the nextLine trap, writing with PrintStream, count-allocate-fill, Lab 11 launch. |
7c, 7d, 7e | wed-05-13-file-io-patterns.pdf |
| Thu 05-14 | Graded array-tracing quiz (15 min). Then throws semantics + the APE four-step warm-up. |
6f for the quiz; warm-up against 7b, 7c. | (lecture) |
| Fri 05-15 | APE File I/O deep dive: walk all six Practice File I/O methods with the four-step pattern. | 7h, 7e | (lecture) |
The week’s load-bearing skills
These are the items that lab tests, quiz questions, and exam problems will all draw from:
new Scanner(new File(path))for the read,new PrintStream(new File(path))for the write. Onethrows FileNotFoundExceptiononmaincovers both.- The
hasNext*cascade in the right order: int → double → general token. The strictest check goes first. - The leftover-newline trap fix: one throwaway
sc.nextLine();between anynextInt/nextDoubleand a followingnextLine. - Count-allocate-fill: pass 1 counts tokens with a fresh
Scanner, pass 2 fills a freshly allocated array with another freshScanner. - Close every Scanner and every PrintStream. The empty-output-file bug is almost always a missing
close(). - The APE four-step pattern (validate, operate, handle errors, update state). Tag the spec sentences before writing code.
Common pitfalls already showing up in office hours
new File(path)does not open the file. It holds a path string. Reading happens when the path is handed to aScanner.- Working-directory trap.
new File("scores.txt")resolves against the JVM launch directory, not the source file directory. UsegetAbsolutePath()to debug. - Forgetting
throws FileNotFoundException. The compiler error names the missing keyword. The fix is one declaration onmain, not atry/catch(that is CSCD 211). hasNextDoublebeforehasNextInt. Every int passeshasNextDouble, so the cascade swallows ints as doubles. Order matters.
After class
- Read 7c, 7d, and 7e in full.
- Start Lab 11. The
countLinesmethod is the first one to write; the spec is a direct application of the count phase of count-allocate-fill. - Drill the array-tracing problems in lesson 6f for Thursday’s quiz.