@ParametersAreNonnullByDefault public final class MorePaths extends Object
Path
utility methods
Unless otherwise noted, all methods in this class do not accept null
arguments and will throw a NullPointerException
if a null argument
is passed to them.
Modifier and Type | Method and Description |
---|---|
static Path |
normalize(Path path)
Normalizes the given path
|
static Path |
resolve(Path path1,
Path path2)
Resolve a path against another path with a potentially different
FileSystem or FileSystemProvider |
@Nonnull public static Path resolve(Path path1, Path path2)
FileSystem
or FileSystemProvider
Path.resolve(Path)
will refuse to operate if its argument is
issued from a different provider (with a ProviderMismatchException
); moreover, if the argument is issued from the
same provider but is on a different filesystem, the result of the
resolution may be on the argument's filesystem, not the caller's.
This method will attempt to resolve the second path against the first
so that the result is always associated to the filesystem (and
therefore provider) of the first argument. For the resolution to operate,
the following conditions must be met for path2
:
If the conditions above are not satisfied, this method throws an
UnresolvablePathException
(unchecked).
If both paths are issued from the same filesystem, this method will
delegate to path1
's .resolve()
; if they are from
different filesystems but share the same provider, this method returns:
path1.resolve(path1.getFileSystem().getPath(path2.toString()))
This means that for instance it is possible to resolve a Unix path against a Windows path, or the reverse, as long as the second path is not absolute (the root paths of both filesystems are incompatible):
foo/bar/baz
against c:
will return
c:\foo\bar\baz
;baz\quux
against /foo/bar
will return
/foo/bar/baz/quux
.path1
- the first pathpath2
- the second pathUnresolvablePathException
- see descriptionInvalidPathException
- path2
is from a different provider,
and one of its name elements is invalid according to path1
's
filesystemFileSystem.getPath(String, String...)
,
FileSystem.getRootDirectories()
@Nonnull public static Path normalize(Path path)
On Unix systems and at least up to Oracle JDK 7u51 and 8u15,
Paths.get("").normalize()
will throw an ArrayIndexOutOfBoundsException
. See this bug
entry.
This method works around the problem by testing that the path is empty (its string representation is empty); if this is the case, it is returned as is. Otherwise, normalization is performed normally.
path
- the path