Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

nio.file.Paths: practical reference

nio.file.Paths: practical reference: Paths is a factory delegating path construction to the default file system or a URI provider.

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

Syntax

java.nio.file.Paths

Paths is a factory delegating path construction to the default file system or a URI provider.

Prefer Path.of for new code; use Paths.get only as the equivalent legacy factory for path strings or provider-backed URI conversion.

A string path is interpreted by the default file-system provider and is platform-dependent. A URI needs an installed provider for its scheme and may require an already-created file system.

An invalid string throws InvalidPathException. A URI with an unsupported scheme or unavailable file system fails differently, so it is not interchangeable with an ordinary path string.

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.Paths.get("a", "b");
        System.out.println(path.getNameCount());
    }
}

Result

2

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

Sources