Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

nio.file.Path: practical reference

nio.file.Path: practical reference: Path is an immutable file-system path value; it need not name an existing file until an I/O operation resolves it.

Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime

Syntax

java.nio.file.Path

Path is an immutable file-system path value; it need not name an existing file until an I/O operation resolves it.

Use Path as a provider-specific, immutable path value and perform I/O separately through Files only when existence or real-file semantics are required.

normalize is purely syntactic and does not resolve symbolic links; toAbsolutePath can depend on process state, while toRealPath accesses the file system and requires the target to exist.

An empty path addresses the default directory. resolve returns an absolute argument unchanged, and relativize requires compatible paths from the same provider.

The example exercises the documented element in a small deterministic program and compares exact standard output.

Examples

public class Main {
    public static void main(String[] args) throws Exception {
        java.nio.file.Path path = java.nio.file.Path.of("docs", "guide.txt");
        System.out.println(path.getNameCount());
        System.out.println(path.getFileName());
    }
}

Result

2
guide.txt

The example exercises the documented element in a small deterministic program and compares exact standard output.

Sources