Series 3: File I/O
Reading and writing files with Scanner and PrintStream, the count-allocate-fill array pattern, the JDK I/O class zoo, and the APE four-step pattern.
Lessons
7a: From File to Array (and Back)
~14 min · Week 7 · Prereq: lessons 6a-6f
The mental model for the unit: read a file into an array, do Week 6 array work in the middle, write the result. Two new APIs, one new exception keyword, the rest is unchanged.
7b: Reading a File with Scanner
~16 min · Week 7 · Prereq: 7a
The File class as a path (not the contents). new Scanner(new File(path)). throws FileNotFoundException as the course route. The canonical EOF loop.
7c: The next* Cascade and the nextLine Trap
~18 min · Week 7 · Prereq: 7b
Four readers (next, nextLine, nextInt, nextDouble). The hasNextInt before hasNextDouble rule. The leftover-newline trap with the throwaway-nextLine() fix.
7d: Writing Files with PrintStream
~13 min · Week 7 · Prereq: 7b
System.out is already a PrintStream. Pointing one at a file is the only new step. Closing matters; the empty-output-file bug is almost always a missing close().
7e: From File to Array: Count-Allocate-Fill
~18 min · Week 7 · Prereq: 7d
The dominant array-from-file pattern. Walk the file twice: once to count, once to fill. End-to-end worked example. First taste of the four-step APE pattern.
7f: The I/O Class Recognition Guide
~18 min · Week 7 · Prereq: 7e
Why the JDK has so many reader and writer classes. BufferedReader, FileReader, PrintWriter, BufferedWriter, FileWriter, Files.readString, Files.writeString. Byte streams vs character streams at recognition level.
7g: File-Class Power Tools
~16 min · Week 7 · Prereq: 7d
Append mode, parent-directory creation with getParentFile().mkdirs(), file inspection (isFile, isDirectory, length, lastModified), and management (delete, renameTo, listFiles).
7h: APE File I/O Walkthrough
~24 min · Week 7 · Prereq: 7e, 7g
The capstone. All six APE Practice File I/O exercises walked sentence-by-sentence with the four-step pattern. Construct-recognition decoder ring, the close-before-null rule, specificity ordering, and the JUnit-over-Javadoc rule.
Previous series: ← Series 2: Arrays & Algorithms
Next series: Series 4: Object-Oriented Programming →