Introduction to Files

Understanding Why and What Files Are

Module 3 • Lesson 1 of 5

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?

💡 Definition

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.

Memory vs File Storage
RAM (Memory)

Temporary

Lost when program ends

Fast access

vs
File (Disk)

Permanent

Survives program end

Slower access

Why Do We Need Files?
  • 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:

📄 Text-Based Files

Human Readable

  • Can open in Notepad
  • See readable characters
  • Easy to edit manually
  • Stores data as text
💻 Binary Files

Computer Readable

  • 🔬 Raw bytes and bits
  • 🔬 Not human readable
  • 🔬 Smaller file size
  • 🔬 Faster to process

Examples of Text Files

📄
.txt
Plain text
💻
.java
Java source code
🌐
.html
Web pages
📋
.csv
Data tables
📝
.xml
Structured data

Examples of Binary Files

.exe
Programs
🎨
.png/.jpg
Images
🎥
.mp4
Videos
🎵
.mp3
Audio
📦
.class
Java bytecode
📚 In This Module

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.

Example: What's Inside a Text File?

If a file contains: Hello

H
72
e
101
l
108
l
108
o
111

Each character is stored as its ASCII code number

Fun Fact

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

Key Takeaways
  • 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...