JavaFile Handling

File Handling

Before you can read or write a file's contents, Java needs a way to represent where that file is and ask questions about it. That is the job of java.io.File — the classic, original class for representing a path to a file or directory on disk.

A File is a path, not the content
A `File` object does not hold a file's contents in memory and does not open any stream. It represents the *path* and lets you query metadata — does it exist, is it a directory, how large is it — and perform simple filesystem operations like creating or deleting. Actually reading or writing bytes/text is the job of separate stream classes, covered on the **Reading & Writing Files** page.
Checking Existence and Metadata

Inspecting a file with File

Java
import java.io.File;

public class FileInfoDemo {
    public static void main(String[] args) {
        File file = new File("notes.txt");

        System.out.println("Exists: " + file.exists());
        System.out.println("Is directory: " + file.isDirectory());
        System.out.println("Is file: " + file.isFile());
        System.out.println("Length (bytes): " + file.length());
        System.out.println("Absolute path: " + file.getAbsolutePath());
    }
}
Common File Methods

Method

Purpose

.exists()

True if the path already exists on disk

.isDirectory()

True if the path is a directory

.isFile()

True if the path is a regular file

.length()

Size in bytes (for a file)

.delete()

Deletes the file or empty directory, returns success as boolean

.mkdir()

Creates one directory; fails if the parent does not exist

.mkdirs()

Creates the directory and any missing parent directories

.listFiles()

Returns the contents of a directory as a File[]

Creating Directories

.mkdir() vs. .mkdirs()

Java
import java.io.File;

public class MakeDirsDemo {
    public static void main(String[] args) {
        File single = new File("output");
        System.out.println(single.mkdir()); // works only if "." already exists

        File nested = new File("output/2026/july");
        System.out.println(nested.mkdirs()); // creates every missing parent too
    }
}
Listing Directory Contents

Listing files in a directory

Java
import java.io.File;

public class ListFilesDemo {
    public static void main(String[] args) {
        File directory = new File(".");
        File[] entries = directory.listFiles();

        if (entries != null) {
            for (File entry : entries) {
                String kind = entry.isDirectory() ? "[dir]" : "[file]";
                System.out.println(kind + " " + entry.getName());
            }
        }
    }
}
[dir] output
[file] notes.txt
[file] pom.xml
The older API
`java.io.File` has been part of Java since version 1.0, and it shows: error handling is weak (many operations just return `false` on failure instead of explaining why), it has no real support for symbolic links, and listing a huge directory loads every entry into memory at once. Modern code increasingly favors `java.nio.file.Path` and the `Files` utility class instead — see the **NIO (New I/O)** page for the newer approach.
  • File represents a path and its metadata — it does not read or write content itself.

  • .exists(), .isDirectory(), .length(), and .delete() are common metadata/management operations.

  • .mkdir() creates one directory; .mkdirs() creates it plus any missing parent directories.

  • For new code, prefer the modern java.nio.file.Path/Files API covered on the NIO page.