In-depth analysis of the Path Interface – NIO.2 APIs

by | Sep 17, 2021 | code fussion

Beginning with JDK 7, the NIO system was substantially expanded and enhanced. In addition, to support for the try-with-resources statement (which provides automatic resource management), the improvements included three new packages (java.nio.file, java.nio.file.attribute, and java.nio.file.spi); several new classes, interfaces, and methods; and direct support for stream-based I/O. The additions have greatly expanded the ways in which NIO can be used, especially with files. Several of the key additions are described in the following sections.

The Path Interface

Perhaps the single most important addition to the NIO system was the Path interface because it encapsulates a path to a file. As you will see, Path is the glue that binds together many of the NIO.2 file-based features. It describes a file’s location within the directory structure. The path is packaged in java.nio.file, and it inherits the following interfaces: Watchable, Iterable, and Comparable. Watchable describes an object that can be monitored for changes.

Creating a path

  • using the factory method -> of() -> public static Path of(String first, String... more)
  • passing an instance of the URI Class to the factory method of() -> path p1 = path.of(new URI("file:/PATH/PATH")) a URI begins with a schema that indicates the resource path followed by a path
  • Obtaining a path using the paths class – method get() -> path p1 = Paths.get("path/p2") you can also pass an instance of the URI to the gt method of the Paths class
  • obtaining a path from the FileSystems class: the FileSystems.getDefault() is an abstract method in the FileSystems class that returns an instance of FileSystem. The FileSystem class includes methods for working with FileSystem directly. getPath(String path..) method is in the FileSystem class and returns an instance of the path interface -> Path path1 = FileSystems.getDefault().getPath("pandas/cuddly.png");
  • obtaining a path from the java.io.File class -> File file = new File("husky.png"); - Path path = file.toPath();

Path Methods

  • toString()
  • int getNameCount()
  • Path getName(int index)
  • Path subPath(int beginIndex, int endIndex) – 0-indexed & does not include the root
  • Path getFileName()
  • Path getParent() – does not print if no parent
  • Path getRoot() – prints null if not absolute path
  • boolean isAbsolute()
  • Path toAbsolute()
  • Path resolve(Path other) – you cannot combine two absolute paths using resolve
  • Path resolve(String path) – join two paths path1.resolve(path2) Path path3 = Path.of("/turkey/food"); System.out.println(path3.resolve("/tiger/cage")); // /tiger/cage
  • Path relativize() - path1.relativize(path2) - you have two paths and you want to know how many steps it would take from one path to the other
  • Path normalize() – path1.normalize() – removes redundant symbols where necessary
  • Path toRealPath(LinkOption… options) throws IOException – eliminates any redundant path symbols, joins relative path with a current working directory, and throws an exception if the path does not exist

You might wonder, why is Path an interface? When a Path is created, the JVM returns a file system�”specific implementation, such as a Windows or Unix Path class. In the vast majority of circumstances, we want to perform the same operations on the Path , regardless of the file system. By providing Path as an interface using the factory pattern, we avoid having to write complex or custom code for each type of file system.

0 Comments

Submit a Comment

Written by Lee N

Lee N is a Certified System Architect, Certified Cloud Engineer, Certified Oracle Database Programmer, and RedHat Administrator II Expert with 5 years of experience in designing, developing, and monitoring web-based systems.

We are Experts in Data Modeling & Intelligent Systems

We develop fast, secure, & reliable systems to simplify business transactions and model data to discover useful information for business decision making. read more

Related Articles

Sealed Classes and Interfaces

Sealed Classes and Interfaces

Sealed classes and interfaces are most applicable to developers of API libraries in which subclasses and subinterfaces must be strictly controlled.

read more

Stay Up to Date With The Latest Technology Updates

Lenhac Limited

Developing world-class software solutions designed to meet your specialized needs to streamline your business operations.

Join Our Newsletter

Stay updated on Ways to Leverage Your Network for Rapid Business Growth using technology

Follow Us

Let’s get social

×