Java File Handling
File handling is a fundamental concept in Java programming. Many real-world applications need to store, retrieve, and manage data from files, such as configuration files, logs, reports, or user-generated content.
Java provides built-in classes and methods that make it easy to create, read, update, and delete files. These capabilities are part of Java’s Input/Output (I/O) system, mainly located in the java.io package.
In this guide, you will learn how Java interacts with files and how the File class helps manage file operations.
Java File Handling
Java includes the File class, which allows programs to work with files and directories on the system.
The File class does not directly read or write file content. Instead, it provides methods to:
- Create files and directories
- Check file properties
- Retrieve file information
- Delete files or folders
- To use the File class, you must import it from the java.io package.
Creating a File Object
To begin working with files, you first create an object of the File class and specify the file name or file path.
Example: Creating a File Object
import java.io.File;
public class FileInfoExample {
public static void main(String[] args) {
File file = new File("sample_data.txt");
System.out.println("File name: " + file.getName());
System.out.println("File path: " + file.getAbsolutePath());
}
}
Explanation
- File file = new File("sample_data.txt"); creates a File object that represents a file named
- sample_data.txt.
- The file may or may not already exist on the system.
- The program simply creates a reference to the file location
Example: Checking File Properties
The following program demonstrates how to check whether a file exists and retrieve information
about it
import java.io.File;
public class FilePropertiesDemo {
public static void main(String[] args) {
File file = new File("project_notes.txt");
if (file.exists()) {
System.out.println("File name: " + file.getName());
System.out.println("Absolute path: " + file.getAbsolutePath());
System.out.println("File size (bytes): " + file.length());
System.out.println("Readable: " + file.canRead());
System.out.println("Writable: " + file.canWrite());
} else {
System.out.println("The specified file does not exist.");
}
}
}
Output (Example)
File name: project_notes.txt
Absolute path: C:\Users\Documents\project_notes.txt
File size (bytes): 120
Readable: true
Writable: true
This example checks whether the file exists before accessing its details, which is a good
programming practice.
Working with Directories
The File class can also be used to work with directories.
Example: Listing Files in a Directory
import java.io.File;
public class DirectoryListExample {
public static void main(String[] args) {
File folder = new File("documents");
String[] files = folder.list();
if (files != null) {
for (String fileName : files) {
System.out.println(fileName);
}
} else {
System.out.println("Directory not found or empty.");
}
}
}
This program retrieves and displays the names of all files inside a directory