B2.5.1 · File processing
File operations
By the end you can
Open, read, write, append, and close sequential text and CSV files with exception handling and resource flushing.
Variables stored in RAM vanish as soon as a program terminates or power is lost. To achieve , computer systems store information in secondary storage (SSDs, hard drives) as files.
The sequential file lifecycle#
Every file operation follows a strict four-stage lifecycle:
- Open: The program requests a from the operating system, specifying the path and the mode of operation.
- Read or Write: The program reads lines sequentially into memory or writes formatted text into buffer streams.
- Flush / Handle Exceptions: Any I/O errors (such as disk full or permissions denied) must be intercepted using exception handling.
- Close: The file stream is closed, flushing all remaining cached data onto disk and releasing OS locks.
Read, Write, and Append modes#
- Read mode (
"r"): Opens an existing file. If the file is missing, aFileNotFoundExceptionis thrown. The file pointer starts at byte 0. - Write mode (
"w"): Creates a new file. If a file with that name already exists, its contents are permanently erased (truncated) without warning. - Append mode (
"a"): Opens a file with the write pointer positioned at the very end, allowing new records to be added without modifying existing data.
Processing CSV files#
Comma-Separated Values (CSV) files store tabular records as plain text lines separated by commas. Processing CSV files combines file reading with string manipulation:
// Reading and parsing CSV lines
while file.hasNextLine()
line = file.readLine()
fields = line.split(",") // Array of comma-delimited tokens
name = fields[0]
score = parseInt(fields[1])
process(name, score)
end whileCheck yourself
File processing
Quick questions, made fresh every time and marked here. Do a few until they feel easy.
Exam practice
Paper 1-style questions
3 questions · 9 marks
Write your answer as you would in the exam, then show the mark points and tick each one your answer made. These are our own questions in the style of the exam, not past-paper questions.
1.Distinguish between read mode ('r'), write mode ('w'), and append mode ('a') when opening a sequential text file.
[3]Distinguish: Make the differences between two things clear.
2.Explain why it is critical to explicitly close a file after completing write operations.
[2]Explain: Give reasons: say what happens AND why, or how one thing causes another.
3.Construct an algorithm in pseudocode to read lines from a sequential text file, count the total number of non-empty lines, and safely close the file handle.
[4]Construct: Draw or write it out (a table, a diagram, a program).
In a nutshell
- Secondary storage provides data persistence across power loss and program termination.
- The file lifecycle consists of open, read/write, flush/error handling, and close.
- Read mode ("r") reads existing files; write mode ("w") truncates and overwrites; append mode ("a") writes to the end.
- Closing files flushes cached RAM buffers onto disk and releases operating system file locks.
