ZetCode

Java File Class

Last modified: April 16, 2025

The java.io.File class represents file and directory pathnames in Java. It provides methods for file system operations like creating, deleting, and renaming files. The class also offers information about file attributes.

File objects are immutable - once created, the abstract pathname they represent cannot change. The class handles both files and directories. It's important to note that File doesn't handle file content operations.

File Class Overview

File provides methods for file system navigation and manipulation. Key operations include checking file existence, permissions, and attributes. The class works with both relative and absolute paths across platforms.

public class File implements Serializable, Comparable {
    public File(String pathname);
    public File(String parent, String child);
    public File(File parent, String child);
    public File(URI uri);
    
    // Common methods
    public boolean exists();
    public boolean canRead();
    public boolean canWrite();
    public boolean isFile();
    public boolean isDirectory();
    public long length();
    public boolean createNewFile();
    public boolean delete();
    public String[] list();
    public File[] listFiles();
    public boolean mkdir();
    public boolean mkdirs();
    public boolean renameTo(File dest);
    public String getAbsolutePath();
    public String getName();
    public String getParent();
    public long lastModified();
}

The code above shows key constructors and methods of File class. These methods allow interaction with the file system. The class implements Serializable and Comparable interfaces for additional functionality.

Creating File Objects

File objects can be created in several ways using different constructors. You can specify paths as strings or combine parent and child paths. The constructors don't verify if the file actually exists.

Main.java
import java.io.File;

public class Main {

    public static void main(String[] args) {
        // Using path string
        File file1 = new File("example.txt");
        
        // Using parent and child strings
        File file2 = new File("docs", "report.txt");
        
        // Using parent File and child string
        File parentDir = new File("projects");
        File file3 = new File(parentDir, "Main.java");
        
        System.out.println("File1 path: " + file1.getPath());
        System.out.println("File2 path: " + file2.getPath());
        System.out.println("File3 path: " + file3.getPath());
        
        // Check if files exist
        System.out.println("File1 exists: " + file1.exists());
        System.out.println("File2 exists: " + file2.exists());
        System.out.println("File3 exists: " + file3.exists());
    }
}

This example demonstrates different ways to create File objects. The getPath method returns the path used to create the File. The exists method checks if the file actually exists in the file system. Paths can be relative or absolute.

Checking File Properties

The File class provides methods to check various file properties. You can verify if a file exists, is readable, writable, or executable. Other methods check if the path represents a file or directory.

Main.java
import java.io.File;

public class Main {

    public static void main(String[] args) {
        File file = new File("example.txt");
        
        System.out.println("File exists: " + file.exists());
        System.out.println("Is file: " + file.isFile());
        System.out.println("Is directory: " + file.isDirectory());
        System.out.println("Is readable: " + file.canRead());
        System.out.println("Is writable: " + file.canWrite());
        System.out.println("Is executable: " + file.canExecute());
        System.out.println("File size (bytes): " + file.length());
        System.out.println("Last modified: " + file.lastModified());
        
        File dir = new File("docs");
        System.out.println("\nDirectory exists: " + dir.exists());
        System.out.println("Is directory: " + dir.isDirectory());
    }
}

This example shows how to check various file properties. The methods return boolean values indicating file attributes. length returns file size in bytes. lastModified returns timestamp in milliseconds since epoch. Directory checks work similarly to file checks.

Creating and Deleting Files

The File class provides methods to create new files and delete existing ones. createNewFile atomically creates a file if it doesn't exist. delete removes files or empty directories. These operations may throw SecurityException.

Main.java
import java.io.File;
import java.io.IOException;

public class Main {

    public static void main(String[] args) {
        File newFile = new File("newfile.txt");
        
        try {
            if (newFile.createNewFile()) {
                System.out.println("File created: " + newFile.getName());
            } else {
                System.out.println("File already exists.");
            }
            
            // Delete the file
            if (newFile.delete()) {
                System.out.println("Deleted the file: " + newFile.getName());
            } else {
                System.out.println("Failed to delete the file.");
            }
            
        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

This example demonstrates file creation and deletion. createNewFile returns true if it created the file. delete returns true if successful. Both methods are atomic operations. IOException may occur if creation fails due to I/O errors.

Working with Directories

The File class can create and manage directories. mkdir creates a single directory, while mkdirs creates all necessary parent directories. list and listFiles methods retrieve directory contents.

Main.java
import java.io.File;

public class Main {

    public static void main(String[] args) {
        // Create single directory
        File dir1 = new File("newdir");
        if (dir1.mkdir()) {
            System.out.println("Directory created: " + dir1.getName());
        }
        
        // Create directory hierarchy
        File dir2 = new File("parent/child/grandchild");
        if (dir2.mkdirs()) {
            System.out.println("Directories created: " + dir2.getPath());
        }
        
        // List directory contents
        File currentDir = new File(".");
        System.out.println("\nContents of current directory:");
        String[] files = currentDir.list();
        for (String file : files) {
            System.out.println(file);
        }
        
        // List files with File objects
        System.out.println("\nFile objects in current directory:");
        File[] fileObjects = currentDir.listFiles();
        for (File file : fileObjects) {
            System.out.println(file.getName() + 
                (file.isDirectory() ? " (dir)" : " (file)"));
        }
    }
}

This example shows directory operations. mkdirs creates all necessary parent directories. list returns directory contents as strings. listFiles returns File objects for each entry. The example demonstrates both approaches to directory listing.

File Path Operations

The File class provides methods to work with file paths. You can get absolute paths, canonical paths, parent directories, and file names. These methods help in navigating and manipulating file system paths.

Main.java
import java.io.File;
import java.io.IOException;

public class Main {

    public static void main(String[] args) {
        File file = new File("../docs/report.txt");
        
        System.out.println("Path: " + file.getPath());
        System.out.println("Absolute path: " + file.getAbsolutePath());
        
        try {
            System.out.println("Canonical path: " + file.getCanonicalPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        System.out.println("Name: " + file.getName());
        System.out.println("Parent: " + file.getParent());
        
        File parentFile = file.getParentFile();
        if (parentFile != null) {
            System.out.println("Parent file exists: " + parentFile.exists());
        }
    }
}

This example demonstrates path-related methods. getPath returns the original path. getAbsolutePath resolves relative paths. getCanonicalPath resolves all symbolic links and relative references. getName returns the file name portion of the path.

Renaming and Moving Files

The renameTo method can rename or move files. The operation behavior depends on the platform. On Unix systems, it can move files between directories if on the same filesystem. The method returns boolean indicating success.

Main.java
import java.io.File;

public class Main {

    public static void main(String[] args) {
        File oldFile = new File("oldname.txt");
        File newFile = new File("newname.txt");
        
        try {
            // Create the original file
            if (oldFile.createNewFile()) {
                System.out.println("Original file created");
            }
            
            // Rename the file
            if (oldFile.renameTo(newFile)) {
                System.out.println("File renamed successfully");
                System.out.println("New file exists: " + newFile.exists());
                System.out.println("Old file exists: " + oldFile.exists());
            } else {
                System.out.println("Rename failed");
            }
            
            // Clean up
            newFile.delete();
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

This example shows file renaming. renameTo returns true if successful. The operation is atomic when moving within the same filesystem. The method may fail if the destination exists or due to permission issues. Behavior varies across platforms.

Source

Java File Class Documentation

In this article, we've covered the essential methods and features of the Java File class. Understanding these concepts is crucial for working with file system operations in Java applications.

Author

My name is Jan Bodnar, and I am a dedicated programmer with many years of experience in the field. I began writing programming articles in 2007 and have since authored over 1,400 articles and eight e-books. With more than eight years of teaching experience, I am committed to sharing my knowledge and helping others master programming concepts.

List all Java tutorials.