Paths And Files

Path represents a filesystem location. Creating a Path object does not create, open, read, or validate the file at that location.

Separating the location from an operation makes APIs composable. Files provides the actions that inspect or change what a path identifies.

Constructing a path

import java.nio.file.Path;

public class PathProbe {
    public static void main(String[] args) {
        Path path = Path.of("reports", "scores.txt");
        System.out.println(path);
        System.out.println(path.isAbsolute());
    }
}

Output on the course runtime:

reports/scores.txt
false

The separator comes from the platform. isAbsolute confirms that the location is relative to the process working directory.

Useful Path operations create more Path values without touching the filesystem:

Path directory = Path.of("reports");
Path report = directory.resolve("scores.txt");
Path fileName = report.getFileName();

Use Files methods when you actually need to check existence, make directories, or read data.

Path.of("reports", "scores.txt") builds the platform-appropriate path from two components.

Relative paths are resolved from the process's working directory. Use absolute paths only when the deployment environment truly supplies them.

Choose the statement that describes what Path creation does below.