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.
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
| Operation | Purpose | Risk to consider |
|---|---|---|
| Read | Use existing data | The path may be wrong or the file may not exist. |
| Write | Create or replace a file | Existing contents may be erased. |
| Append | Add to the end | Repeated runs may create duplicate records. |
| Close | Release the resource and finish output | Use 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
SelectedCreate 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.
Random Quote
SelectedCreate 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
SelectedA 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.
Till
SelectedUse 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.
Selected challenge
This choice is shared with the portfolio setup page.
Create your challenge folder
Run this command before starting. It creates the correct empty folder inside your portfolio.
Complete the challenge
Write and test your own solution in the folder created above.
Optional scaffolded support
Use only as much support as you need. The templates organise the program but leave the important algorithm unfinished.
Submit for review
Run this when your program is complete. It creates the README, commits the folder and pushes it.