Week 7: File I/O
May 11 – May 15 · 8 lessons · Typed File Statistics lab · APE prep
- 7-a From File to Array (and Back) The story arc: read, work, write
- 7-b Reading a File with Scanner File as path, throws FileNotFoundException, EOF loop
- 7-c The next* Cascade and the nextLine Trap int-before-double, leftover-newline fix
- 7-d Writing Files with PrintStream System.out is a PrintStream, close to flush
- 7-e From File to Array: Count-Allocate-Fill Two passes, the four-step APE pattern
- 7-f The I/O Class Recognition Guide BufferedReader, PrintWriter, byte vs character
- 7-g File-Class Power Tools Append mode, mkdirs, isFile, delete, listFiles
- 7-h APE File I/O Walkthrough Six worked problems with the four-step pattern
Objectives
- Read a file with
new Scanner(new File(path))and declarethrows FileNotFoundExceptiononmain - Loop until end of file with
hasNext,hasNextLine,hasNextInt,hasNextDouble - Order the cascade
hasNextIntbeforehasNextDoubleand recognize the leftover-newline trap on sight - Write a file with
new PrintStream(new File(path)), using the sameprint/println/printfAPI asSystem.out - Apply the count-allocate-fill pattern when the array size depends on the file's contents
- Recognize
BufferedReader,PrintWriter,FileWriter,BufferedWriterin real code without writing them yet - Open a file in append mode and create parent directories with
getParentFile().mkdirs() - Tag each sentence of an APE File I/O spec with its four-step role (validate, operate, handle errors, update state) before writing code
Cadence
- Mon: searching/sorting in a file context (lesson 7a)
- Tue: File class, Scanner-on-File, throws (7b)
- Wed: the cascade and PrintStream (7c, 7d)
- Thu: 15-min array-tracing quiz, then APE warm-up
- Fri: APE File I/O deep dive (7e, 7h)