Sub topic 11
Checking Existence and Reading File Attributes
This chapter assumes you know basic Java classes, Path, Files, streams, and checked exceptions. You also need Java 11 or later.
A Path describes a location. It does not prove that the location exists. Use the Files class to ask questions about that location and to read its metadata, which is information about a file or directory.
Checking whether a path exists
The simplest checks are:
Files.exists(path)returnstruewhen the path exists.Files.notExists(path)returnstruewhen the path is known not to exist.Files.isRegularFile(path)checks for an ordinary file.Files.isDirectory(path)checks for a directory.
import java.nio.file.Files;
import java.nio.file.Path;
public class InspectPath {
public static void main(String[] args) {
Path path = args.length == 0
? Path.of(".")
: Path.of(args[0]);
System.out.println("Path: " + path.toAbsolutePath());
System.out.println("Exists: " + Files.exists(path));
System.out.println("Regular file: " + Files.isRegularFile(path));
System.out.println("Directory: " + Files.isDirectory(path));
}
}These methods return false when the path does not exist. They can also return false when Java cannot obtain the information, such as when permissions prevent access. Therefore, a false result is not always proof that a path is absent.
Files.exists follows symbolic links by default. A symbolic link is a filesystem entry that points to another path. Pass LinkOption.NOFOLLOW_LINKS when you want to inspect the link itself instead of its target.
Reading common attributes
A file's attributes include its size, modification time, and type. Files.size(path) returns the size in bytes. Files.getLastModifiedTime(path) returns a FileTime.
For several related values, use Files.readAttributes. The BasicFileAttributes class provides a consistent set of basic attributes.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
public class FileDetails {
public static void main(String[] args) {
Path path = args.length == 0
? Path.of(".")
: Path.of(args[0]);
try {
BasicFileAttributes attributes =
Files.readAttributes(path, BasicFileAttributes.class);
System.out.println("Absolute path: " + path.toAbsolutePath());
System.out.println("Directory: " + attributes.isDirectory());
System.out.println("Regular file: " + attributes.isRegularFile());
System.out.println("Symbolic link: " + attributes.isSymbolicLink());
System.out.println("Size in bytes: " + attributes.size());
System.out.println("Last modified: " + attributes.lastModifiedTime());
System.out.println("Created: " + attributes.creationTime());
} catch (IOException e) {
System.out.println("Could not read attributes: " + e.getMessage());
}
}
}Reading attributes can fail because the path is missing, permissions are insufficient, or the filesystem reports an error. Handle the resulting IOException rather than relying only on an existence check.
Choosing an attribute method
| Need | Method |
|---|---|
| Check for a path | `Files.exists(path)` |
| Check for a directory | `Files.isDirectory(path)` |
| Get one size value | `Files.size(path)` |
| Get several basic values | `Files.readAttributes(...)` |
Exercise
Why can Files.exists(path) return false even when a path is present?
Show answer
The path may be inaccessible, or a filesystem error may prevent Java from obtaining reliable information.