Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

nio.file.Files: practical reference

nio.file.Files: practical reference: Files provides file operations whose atomicity, attributes, and behavior depend on the selected method and file-system provider.

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

Syntax

java.nio.file.Files

Files provides file operations whose atomicity, attributes, and behavior depend on the selected method and file-system provider.

Use Files for provider-aware file operations, selecting explicit options for replacement, links, attributes, atomic moves, and character encoding.

exists is not an authorization check and creates a time-of-check/time-of-use race. Streams returned by list, walk, lines, or find hold resources and must be closed.

ATOMIC_MOVE and links are provider-dependent optional operations. readAllBytes and readString can exhaust memory on large files, while a failed copy or move need not roll back every partial effect.

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 file = java.nio.file.Files.createTempFile("cktis-java-", ".txt");
        try {
            java.nio.file.Files.writeString(file, "Java");
            System.out.println(java.nio.file.Files.readString(file));
        } finally { java.nio.file.Files.deleteIfExists(file); }
    }
}

Result

Java

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

Sources