Creating Path Objects with the Path.of() Method – Java I/O: Part II

Creating Path Objects with the Path.of() Method

The simplest way to create a Path is to use the static factory method Path.of(String first, String… more). It joins the first string with any strings in the variable arity parameter more to create a path string that is the basis of constructing a Path object. This method creates a Path object in accordance with the default file system.

For instance, the default file system can be queried for the platform-specific name separator for name elements in a path.

The three absolute paths below are equivalent on a Unix platform, as they create a Path object based on the same path string. The nameSeparator string is the platform-specific name separator obtained from the default file system. At (1) and (2) below, only the first string parameter is specified in the Path.of() method, and it is the basis for constructing a Path object. However, specifying the variable arity parameter where possible is recommended when a Path object is constructed as shown at (3), as joining of the name elements is implicitly done using the platform-specific name separator. The equals() methods simply checks for equality on the path string, not on any directory entry denoted by the Path objects.

Click here to view code image

FileSystem dfs = FileSystems.getDefault();     // Obtain the default file system.
String nameSeparator = dfs.getSeparator();     // The name separator for a path.

Path absPath1 = Path.of(“/a/b/c”);                         // (1) /a/b/c
Path absPath2 = Path.of(nameSeparator + “a” +              // (2) /a/b/c
                        nameSeparator + “b” +
                        nameSeparator + “c”);
Path absPath3 = Path.of(nameSeparator, “a”, “b”, “c”);     // (3) /a/b/c
System.out.println(absPath1.equals(absPath2) &&
                   absPath2.equals(absPath3));             // true

The two absolute paths below are equivalent on a Windows platform, as they create Path objects based on the same path string. Note that the backslash character must be escaped with a second backslash in a string. Otherwise, it will be interpreted as starting an escape sequence (ยง2.1, p. 38).

Click here to view code image

Path absPath4 = Path.of(“C:\\a\\b\\c”);                    // (4) C:\a\b\c
Path absPath5 = Path.of(“C:”, “a”, “b”, “c”);              // (5) C:\a\b\c

The Path created below is a relative path, as no root component is specified in the arguments.

Click here to view code image

Path relPath1 = Path.of(“c”, “d”);                         //  c/d

Often we need to create a Path object to denote the current directory. This can be done via a system property named “user.dir” that can be looked up, as shown at (1) below, and its value used to construct a Path object, as shown at (2). The path string of the current directory can be used to create paths relative to the current directory, as shown at (3).

Click here to view code image

String pathOfCurrDir = System.getProperty(“user.dir”);  // (1)
Path currDir = Path.of(pathOfCurrDir);                  // (2)
Path relPath = Path.of(pathOfCurrDir, “d”);             // (3) <curr-dir-path>/d

Leave a Reply

Your email address will not be published. Required fields are marked *