File Handling
Almost every real-world application needs to work with files — reading configuration, saving logs, storing reports, or exporting data.
File Handling in Java allows programs to create, read, write, and manage files efficiently using built-in APIs.
Why File Handling Is Important
Files allow data to persist even after a program stops running. Without file handling, all data would be lost when the application closes.
Common use cases include:
- Saving user data
- Reading configuration files
- Generating reports
- Application logging
File Class Overview
The File class (from java.io) represents a file or directory path.
It does not read or write data directly, but helps manage file information.
import java.io.File;
public class Main {
public static void main(String[] args) {
File file = new File("data.txt");
System.out.println(file.exists());
System.out.println(file.getName());
System.out.println(file.getAbsolutePath());
}
}
Creating a File
You can create a new file using the createNewFile() method.
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
File file = new File("example.txt");
if (file.createNewFile()) {
System.out.println("File created");
} else {
System.out.println("File already exists");
}
}
}
Writing Data to a File
Java provides classes like FileWriter and BufferedWriter
to write data into files.
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
FileWriter writer = new FileWriter("example.txt");
writer.write("Welcome to Java File Handling");
writer.close();
System.out.println("Data written to file");
}
}
Reading Data from a File
To read file content, Java provides classes like FileReader
and BufferedReader.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(
new FileReader("example.txt"));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
}
}
Appending Data to a File
You can append data instead of overwriting existing content.
FileWriter writer = new FileWriter("example.txt", true);
writer.write("\nNew line added");
writer.close();
Real-World Example
Consider an application that logs user activity.
FileWriter log = new FileWriter("app.log", true);
log.write("User logged in at " + System.currentTimeMillis() + "\n");
log.close();
Such logging is common in backend systems.
Handling File Exceptions
File operations can fail due to missing files or permission issues. Always handle exceptions properly.
try {
FileReader reader = new FileReader("missing.txt");
} catch (IOException e) {
System.out.println("File not found or access denied");
}
Best Practices
- Always close file resources
- Use buffered streams for better performance
- Handle exceptions carefully
- Avoid hard-coded file paths
Key Takeaways
- File handling enables data persistence
- Java provides rich file APIs
- Essential for real-world applications
In the next lesson, we will learn about JDBC Basics and how Java connects to databases.