1 A Little Tale About Files
Once upon a time, there was a curious student who wanted to store and retrieve information from the computer.
They asked their wise teacher, "Teacher, how do I keep my data in something that lasts even after my program stops running?"
The teacher smiled and replied, "My dear student, that 'something' is called a file."
This simple question leads us to one of the most important concepts in programming: persistent storage.
2 What Is a File?
A file is a place where we store data permanently (on a disk or other storage). Unlike program memory (RAM) - which is temporary - files hold onto data even after the program stops running.
Temporary
Lost when program ends
Fast access
Permanent
Survives program end
Slower access
- Save user data: Game saves, user preferences, documents
- Store configurations: Application settings that persist
- Process large data: Data too big to fit in memory
- Share information: Exchange data between programs
- Logging: Record program activity for debugging
3 Types of Files
Files can be categorized into two main types based on how they store data:
Human Readable
- ✅ Can open in Notepad
- ✅ See readable characters
- ✅ Easy to edit manually
- ✅ Stores data as text
Computer Readable
- 🔬 Raw bytes and bits
- 🔬 Not human readable
- 🔬 Smaller file size
- 🔬 Faster to process
Examples of Text Files
Examples of Binary Files
We will focus on handling text-based files. Binary file handling requires different techniques that are beyond the scope of this course.
4 How Text Files Store Data
Text files store data as a sequence of characters. Each character is encoded using a standard like ASCII or UTF-8.
If a file contains: Hello
Each character is stored as its ASCII code number
Even though text files store numbers (ASCII codes), we call them "text files" because they're designed to represent human-readable text. When you open them in Notepad, the computer converts these codes back to characters!
5 Summary
- ✓ Files store data permanently on disk, surviving program termination.
- ✓ RAM is temporary; files are permanent storage.
- ✓ Text files are human-readable (.txt, .java, .html, .csv).
- ✓ Binary files are computer-readable (.exe, .png, .mp4).
- ✓ In this module, we focus on text-based file handling in Java.
The teacher concluded, "Now that you understand what files are and why we need them, let's learn how to work with them in Java!"
The student nodded eagerly, ready for the next lesson...