The File Class & Directory Operations in Java
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.
Creating a File object
new File("path/to/thing") represents a path without touching the filesystem at all yet.
exists()
Checks whether something actually exists at that path right now.
isDirectory() and isFile()
Distinguish whether an existing path points to a directory or to a regular file.
mkdir() and mkdirs()
Create a new directory; mkdirs() additionally creates any missing parent directories along the way.
listFiles()
Returns every file and subdirectory directly inside a given directory, the standard way to enumerate its contents.
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()); }}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.