Write a program that asks for your high score, then close it and run it again. The score is gone. Every variable a program makes lives in the computer's memory (RAM) only while the program is running — the moment it ends, all of that memory is handed back and wiped clean. That's fine for a quick calculation, but useless for anything you want to keep: a saved game, a high-score table, a register of pupils, a document you're writing.
The answer is a file. A file is a named lump of data stored on a disk (a hard drive, an SSD, a USB stick), and — crucially — it persists: it stays there after the program ends, after you close the app, even after you switch the computer off. Saving to a file is how a program remembers things between one run and the next.
This page is about how a program gets data into and out of those files.
Whatever the language, working with a file almost always follows the same three-step dance. Think of it like a library book:
Here is what those steps look like in a real program. This is not runnable here — our practice sandbox has no disk to read from or write to — but this is genuinely how the code reads when it runs on a real computer:
The exact names differ between languages (Python uses open() and with;
others use readAll or ReadLine), but the shape is always the same:
open, then read or write, then close.
Most files you'll meet at this level are text files — plain lines of text, one under the next, exactly like the lines in this paragraph. Because the data is naturally arranged as lines, a program usually reads a text file one line at a time, dealing with each line before moving to the next. That's perfect for a list of names, a set of scores, or a log of events:
Notice how neatly this mirrors a
Since our sandbox has no real disk, we'll do the honest next-best thing. When a program reads a
text file, what it ends up with in memory is essentially an array of lines. So
we can put that array in the code directly and process it exactly as we would after
reading a real file. The only difference is where the lines came from — here we type them in;
in a real program the line const lines = file.readLines() would fill this same array
from disk. Everything after that is identical.
Run this — it counts the lines in our "file", just like reading a register:
Once the file's lines are an array, every array trick you know works on them. Here we hunt for the longest line — the "keep the best so far" pattern — which is just the sort of thing you might do to a file of names or of words:
Files of numbers are just as common — a file of daily temperatures, or of sales figures. Each
line holds a number as text, so we turn it into a real number with Number(...)
and add it to a running total (an accumulator):
In a real program you'd read those lines from a file at the top and the loop would be unchanged. Learning to think of a file as "an array of lines I loop over" carries you a very long way.
There are two ways to open a file for writing, and the difference matters enormously:
Append is exactly what you want for a growing record — a log file, a high-score table, a diary — where each run should add to the history, not replace it:
In our array-of-lines model, appending a line is simply lines.push(...) — adding a
new element on the end, leaving the rest untouched:
Two classic file mistakes cost people real data — learn them now.
1. Write mode overwrites the whole file. Opening a file in write mode wipes everything already in it before you type a single character. If you meant to add a score but opened for writing, you've just deleted the entire high-score table:
If you want to keep the old data and add to it, open in append mode instead.
2. Forgetting to close the file. Your writes often sit in a temporary buffer
in memory and are only truly saved to disk when the file is closed. Forget the
close() and your carefully written lines may never reach the disk at all — the
program looks like it worked, but the file is empty or half-written. Always close every
file you open (many languages give you an automatic way to guarantee this, like
Python's with block).