SwadhyAI Notes

All Java notes

Java File Methods Reference

›Short versionOpen if you only have a minute.
  • java.io.File is a pathname. Constructing one does not create a file or a directory on disk.
  • exists, isFile, and isDirectory answer different questions. A path can exist and still not be a normal file.
  • createNewFile creates an empty file and returns false if that path already exists.
  • mkdir creates one directory. mkdirs creates missing parents as well. Both return false on failure.
  • delete deletes a file or an empty directory. It does not throw for a typical failure, and it does not empty a directory first.
  • listFiles returns null when the path is not a directory or cannot be listed. It does not return an empty array for that failure.

java.io.File represents a file or directory pathname. It can inspect, create, rename, and delete filesystem entries, but it does not read or write the contents of a file.

Methods

APIParametersReturnsBehaviorWatch for
exists()None.boolean.Checks whether the pathname currently exists.A false result can be caused by permissions as well as absence.
createNewFile()None.true if created, false if already existed.Atomically creates an empty file.Throws IOException for many filesystem failures.
mkdir() / mkdirs()None.boolean success.Creates one directory or a directory tree.Return false can mean it already exists or creation failed.
listFiles()Optional FileFilter or FilenameFilter.File[] or null.Lists entries inside a directory.Returns null if not a directory or if access fails.
delete()None.boolean success.Deletes a file or empty directory.Does not throw for most failures and does not delete non-empty directories.
toPath()None.Path.Bridges old File API to newer NIO APIs.Path operations may have different exception behavior.

Which call fits

OptionBest forTradeoff
FileLegacy APIs and simple path metadata examples.Weak error reporting through boolean returns.
PathModern path representation with NIO Files utilities.Requires learning java.nio.file APIs.
FilesReading, writing, copying, moving, and walking paths.Static utility style and checked IOException.

Examples

Create parent directories and file

java
File file = new File("logs/app.txt");
File parent = file.getParentFile();

if (parent != null) {
    parent.mkdirs();
}

if (file.createNewFile()) {
    System.out.println("Created " + file.getAbsolutePath());
}

Safely list a directory

java
File dir = new File("logs");
File[] files = dir.listFiles();

if (files != null) {
    for (File file : files) {
        System.out.println(file.getName());
    }
}

File dir = new File("missing"); and dir is not a directory. What does dir.listFiles() return?

null, not an empty array. An empty array means the directory exists and has no entries. Null means the path is not a directory or the listing failed. Check for null before a for-each loop, or the loop throws NullPointerException. delete() has a similar quiet failure: it returns false for a missing path or a non-empty directory and does not throw.