Learn Java
import com.learnjava.filehandling.FileClassOperations;

The File Class & Directory Operations in Java

@Concept

The File class, from java.io, represents a path on the filesystem, a file or a directory, and provides methods to check its state and perform basic operations without necessarily reading or writing its contents: exists() checks whether it's actually there, isDirectory() and isFile() distinguish which kind of thing it is, and mkdir() — or mkdirs() for creating any missing parent directories along the way too — creates a new directory.

A File object can represent a path that doesn't exist at all yet. Creating one doesn't touch the filesystem in any way; it's purely an in-memory representation of a path, and only calling one of its action methods, like mkdir() or delete(), actually causes anything to happen on disk. listFiles() returns every file and subdirectory directly inside a directory, the standard way to enumerate a folder's contents.

@HowItWorks
1

Creating a File object

new File("path/to/thing") represents a path without touching the filesystem at all yet.

2

exists()

Checks whether something actually exists at that path right now.

3

isDirectory() and isFile()

Distinguish whether an existing path points to a directory or to a regular file.

4

mkdir() and mkdirs()

Create a new directory; mkdirs() additionally creates any missing parent directories along the way.

5

listFiles()

Returns every file and subdirectory directly inside a given directory, the standard way to enumerate its contents.

@Example
import java.io.File;
 
public class FileClassDemo {
public static void main(String[] args) {
File dir = new File("demo-folder");
boolean created = dir.mkdir();
System.out.println("Created: " + created);
System.out.println("Is directory: " + dir.isDirectory());
}
}
Output
Created: true
Is directory: true
1dir represents a path, "demo-folder", that doesn't need to exist yet just to create this File object.
2dir.mkdir() actually creates that directory on the filesystem and returns true, confirming it succeeded.
3dir.isDirectory() checks the filesystem again and confirms that path now really does point to a directory.
4Both println calls run once each, confirming the directory was both created and correctly recognized afterward.
@UseCases
Checking whether a file or folder exists before attempting to read from or write to it
Creating a directory structure for output files before writing anything into it
Listing every file inside a folder to process each one in turn
Distinguishing a file from a directory before deciding how to handle a given path

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.