Making your program's data survive after it closes — reading and writing text files to secondary storage.
Core text file operations:
| Operation | Purpose |
|---|---|
| Create | Make a brand new text file, ready to write to |
| Append | Add new content to the end of an existing file, without erasing what's already there |
| Read | Retrieve the content already stored in the file |
Text files may store data in different structural formats:
Example
Text file content (each line = one Learner, fields separated by #): Ana#17#11 Ben#18#12 Reading this file: for each line, split on '#' to extract name, age and grade, then create a Learner object for each and store them in an array of objects.
Testing if a file exists before trying to read it prevents a runtime crash — this typically requires exception handling (see 11.4.14) to gracefully deal with a missing or inaccessible file.
💡 Exam Tip
Always plan your file's delimiter and structure BEFORE writing your read/write code — inconsistent formatting between writing and reading a file is one of the most common practical exam bugs.