LESSON 21
3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Define files and directories in Java.
- Create files using Java.
- Create and manage directories in Java.
- Read basic file information in Java.
- Explain the importance of file handling in Java applications.
3.2 Overview
Many software applications store and manage information using files and directories. Java provides built-in classes that allow applications to create, access, organize, and manage files and folders within computer systems.
This lesson introduces learners to file systems and directories in Java and explains how Java applications interact with files and folders for storage and processing. Learners will also explore how Java manages file paths, directories, and file operations.
File systems and directories are commonly used in:
- banking systems,
- inventory systems,
- healthcare applications,
- enterprise software,
- and document management systems.
Understanding file systems and directories is important because many applications rely on stored data and file processing.
KT2101 — Introduction to Files and Directories
A file is a collection of stored data.
A directory (folder) is a location used to organize files and other directories.
Java uses built-in classes to interact with files and directories.
Importance of Files and Directories
Files and directories help applications:
- store information,
- organize data,
- manage records,
- and support persistent storage.
KT2102 — The File Class
Java uses the File class to manage files and directories.
The File class belongs to:
java.io
Importing the File Class
import java.io.File;
Creating a File Object
File file = new File("example.txt");
Explanation
| Component | Purpose |
|---|---|
| File | Java file handling class |
| example.txt | File name/path |
Importance of the File Class
The File class allows developers to:
- manage files,
- create directories,
- check file properties,
- and process file paths.
KT2103 — Creating Files in Java
Java applications can create files programmatically.
Example
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try {
File file = new File("sample.txt");
if(file.createNewFile()) {
System.out.println("File created");
}
} catch(IOException e) {
System.out.println("Error creating file");
}
}
}
Output
File created
Importance of File Creation
File creation is used for:
- saving records,
- generating reports,
- storing logs,
- and managing application data.
KT2104 — Creating Directories in Java
Java applications can create directories using the mkdir() method.
Example
import java.io.File;
public class Main {
public static void main(String[] args) {
File folder = new File("Documents");
if(folder.mkdir()) {
System.out.println("Directory created");
}
}
}
Output
Directory created
Importance of Directories
Directories help:
- organize files,
- separate data,
- and manage application storage structures.
KT2105 — Reading File Information
The File class provides methods for retrieving file information.
Common File Methods
| Method | Purpose |
|---|---|
| getName() | Returns file name |
| exists() | Checks whether file exists |
| length() | Returns file size |
| canRead() | Checks read permission |
| canWrite() | Checks write permission |
Example
import java.io.File;
public class Main {
public static void main(String[] args) {
File file = new File("sample.txt");
System.out.println(file.getName());
System.out.println(file.exists());
}
}
Example Output
sample.txt
true
Importance of File Information
File information methods help applications:
- validate files,
- check permissions,
- and manage storage safely.
KT2106 — File Paths in Java
A file path specifies the location of a file or directory.
Example Path
C:\Documents\sample.txt
Relative Path Example
sample.txt
Absolute vs Relative Paths
| Absolute Path | Relative Path |
|---|---|
| Full file location | Location relative to current directory |
| Longer path | Shorter path |
Importance of File Paths
File paths help applications:
- locate files,
- organize storage,
- and access system resources.
KT2107 — Real-World Use of Files and Directories
Files and directories are used in:
- banking systems,
- document systems,
- healthcare applications,
- inventory systems,
- and enterprise software.
Examples:
- customer records,
- transaction logs,
- report files,
- and system backups.
File systems support data storage and organization in software applications.
3.5 Key Notes / Summary
- Files store data permanently.
- Directories organize files and folders.
- Java uses the File class for file management.
- createNewFile() creates files.
- mkdir() creates directories.
- File methods retrieve file information.
- File paths specify file locations.