File Processing
IB Syllabus: B2.5 – File processing
Overview
Programs need to persist data – to save information that survives after the program ends and to load information saved by previous runs. File processing is how programs read from and write to text files on disk.
| Sub-page | What you’ll learn |
|---|---|
| Reading Files | Scanner, BufferedReader, parsing lines, processing data from files |
| Writing Files | FileWriter, BufferedWriter, overwrite vs append, CSV output |
When to Use Which
| Task | Best Approach |
|---|---|
| Read a small file with mixed types (int, String) | Scanner(new File(...)) |
| Read a large text file line-by-line | BufferedReader(new FileReader(...)) |
| Write to a file (overwrite) | FileWriter("file.txt") |
| Append to a file | FileWriter("file.txt", true) |
Write efficiently with newLine() | BufferedWriter(new FileWriter(...)) |
| Always | Wrap in try/catch, close the file |