Programming Companion
Java

Programming technique · B2.5.1

File processing

Files allow data to remain available after a program ends. A program must choose whether to read, overwrite or append, and it must handle unavailable resources safely.

IB DP CS standard B2.5.1: Construct code to open, read, write, append and close sequential text files.
The same file can be opened for different purposes
flowchart TD A[Program] --> B[Open text file] B --> C{Which operation?} C -- Read --> D[Read existing lines] C -- Write --> E[Replace file contents] C -- Append --> F[Add new content at the end] D --> G[Close reliably] E --> G F --> G B -. unavailable .-> H[Handle the exception and explain the problem]

Use a resource-management structure so the file closes even when an error occurs.

Write a text file

Opening a FileWriter without append mode overwrites the existing file. Try-with-resources closes the file automatically.

Opening with mode "w" creates or overwrites the file. A with block closes it automatically when the block ends.

Append instead of overwrite

Pass true to the FileWriter constructor to add new content at the end.

Use mode "a" to add new content at the end without replacing existing records.

Read line by line

OperationPurposeRisk to consider
ReadUse existing dataThe path may be wrong or the file may not exist.
WriteCreate or replace a fileExisting contents may be erased.
AppendAdd to the endRepeated runs may create duplicate records.
CloseRelease the resource and finish outputUse try-with-resources or with so closure happens reliably.

Use a clear record format

Choose a delimiter or line structure that your reading code can interpret consistently.

Keep paths simple

During development, place small data files in a predictable project folder and report the path when debugging.

Handle empty files

A file can exist but contain no usable records.

Challenges Choose one

Choose a challenge that feels appropriate for you. Code heat is only a rough estimate, not a fixed level.

Quote of the Day

Challenge ID: PC-T17-C01 · Standards: B2.5.1

Create a text file containing one quotation. Write a program that opens the file, reads the quotation and outputs it with a clear heading. Handle a missing file without crashing.

A document and computer screen representing a quotation read from a file.

Random Quote

Challenge ID: PC-T17-C02 · Standards: B2.2.2, B2.5.1

Create a text file containing several quotations, one per line. Read every line into a collection and output one randomly selected quotation. Give a clear message when the file is empty or unavailable.

Product Catalogue

Challenge ID: PC-T17-C03 · Standards: B2.3.3, B2.5.1

A product has a code, description and price. Repeatedly ask the user for product data until no code is entered, then append each product to a text catalogue using one consistent record format. Add a menu option that reads and displays the complete catalogue.

A digital product catalogue with product cards and price fields.

Till

Challenge ID: PC-T17-C04 · Standards: B2.2.2, B2.3.2, B2.3.3, B2.3.4, B2.5.1

Use a product catalogue text file to create a simple till. In trading mode, enter product codes, find matching records, display descriptions and prices, maintain a subtotal, accept payment and calculate change. In admin mode, allow the user to view the catalogue and append a new product. Separate file operations, searching and transaction logic into methods.

Scaffold available
A point-of-sale till with products, a subtotal and a receipt.