Converting Path Objects – Java I/O: Part II

Converting Path Objects

The Path interface provides many methods for converting paths. A majority of these methods manipulate the path strings syntactically and do not assume that the path strings of the Path objects denote actual directory entries in the file system.

Path toAbsolutePath()

Returns a Path object representing the absolute path of this Path object. If this Path object represents a relative path, an absolute path is constructed by appending it to the absolute path of the current directory. If this Path is an absolute path, it just returns this Path object.

Path normalize()

Returns a Path object that is created from this Path object with redundant name elements eliminated. This typically involves eliminating the “.” and “dir/..” strings in this Path object, as these do not change the hierarchy of a path. The method applies the elimination procedure repeatedly until all such redundancies are eliminated.

Click here to view code image

Path resolve(Path other)
default Path resolve(String other)

These method resolve the given Path object (either specified or created from the specified string) against this Path object.

If the other Path object represents an absolute path, the other Path object is returned as the result, regardless of whether this Path object represents an absolute or a relative path. Otherwise, the method creates a result Path object by joining the other Path object to this Path object.

Click here to view code image

default Path resolveSibling(Path other)
default Path resolveSibling(String other)

Resolve the given Path object (either specified or created from the specified string) against this Path object’s parent path. That is, they resolve the given Path object by calling the resolve(other) method on this Path object’s parent path.

Path relativize(Path other)

Constructs a relative Path object between this Path object and the given other Path object. The constructed relative Path object when resolved against this Path object should yield a path that represents the same directory entry as the given other Path object.

A relative Path object can only be constructed when this Path object and the given Path object both represent either absolute paths or relative paths.

If this path is /w/x and the given path is /w/x/y/z, the resulting relative path is y/z. That is, the given path /w/x/y/z and the resulting relative path y/z represent the same directory entry.

For two Path objects that are equal, the empty path is returned.

The relativize() method is the inverse of the resolve() method.

Click here to view code image

Path toRealPath(LinkOption… options) throws IOException

Returns a Path object that represents the real path of an existing directory entry. Note that this method throws an IOException if the path does not exist in the file system. It converts the path to an absolute path and removes any redundant elements, and does not follow symbolic links if the enum constant Link-Option.NOFOLLOW_LINKS is specified (p. 1301).

Categories: , ,

Leave a Reply

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