Skip to content

Java NIO.2 Utilities

The Java NIO.2 (Non-blocking I/O 2) API introduces utility classes designed to simplify file system operations and path management. These utilities generally replace or augment older java.io.File functionalities by offering more robust exception handling, support for symbolic links, and file attribute management^[600-developer__java__java-base__java-util.md].

Core Utility Classes

The API establishes a correspondence between legacy classes and their NIO.2 replacements, moving from stateful objects to stateless utility classes^[600-developer__java__java-base__java-util.md].

java.nio.file.Paths

The Paths class serves as the primary utility for working with paths^[600-developer__java__java-base__java-util.md]. It provides static methods, such as get(), to convert path strings or URIs into Path objects. This class acts as a factory for Path instances, abstracting the underlying file system implementation^[600-developer__java__java-base__java-util.md].

java.nio.file.Files

The Files class is the counterpart to the legacy java.io.File class^[600-developer__java__java-base__java-util.md]. It provides a rich set of static methods for file operations, including:

  • File Creation and Deletion: Methods for creating files and directories, as well as deleting them.
  • Copying and Moving: Utilities for copying and moving files between locations, often with options to replace existing files or copy attributes.
  • I/O Operations: Helpers for reading and writing files, such as readAllLines(), write(), and readAllBytes(), which simplify stream handling.
  • Attributes: Methods for checking file existence, readability, writability, and setting permissions.
  • Java I/O
  • [[Path interface]]
  • [[File Systems]]

Sources

^[600-developer__java__java-base__java-util.md]