Path

expect class Path : Comparable<Path>

A hierarchical address on a file system. A path is an identifier only; a FileSystem is required to access the file that a path refers to, if any.

UNIX and Windows Paths

----------------------

Paths follow different rules on UNIX vs. Windows operating systems. On UNIX operating systems (including Linux, Android, macOS, and iOS), the / slash character separates path segments. On Windows, the \ backslash character separates path segments. The two platforms each have their own rules for path resolution. This class implements all rules on all platforms; for example you can model a Linux path in a native Windows application.

Absolute and Relative Paths

---------------------------

  • Absolute paths identify a location independent of any working directory. On UNIX, absolute paths are prefixed with a slash, /. On Windows, absolute paths are one of two forms. The first is a volume letter, a colon, and a backslash, like C:\. The second is called a Universal Naming Convention (UNC) path, and it is prefixed by two backslashes \\. The term ‘fully-qualified path’ is a synonym of ‘absolute path’.

  • Relative paths are everything else. On their own, relative paths do not identify a location on a file system; they are relative to the system's current working directory. Use FileSystem.canonicalize to convert a relative path to its absolute path on a particular file system.

There are some special cases when working with relative paths.

On Windows, each volume (like A:\ and C:\) has its own current working directory. A path prefixed with a volume letter and colon but no slash (like A:letter.doc) is relative to the working directory on the named volume. For example, if the working directory on A:\ is A:\jesse, then the path A:letter.doc resolves to A:\jesse\letter.doc.

The path string C:\Windows is an absolute path when following Windows rules and a relative path when following UNIX rules. For example, if the current working directory is /Users/jesse, then C:\Windows resolves to /Users/jesse/C:/Windows.

This class decides which rules to follow by inspecting the first slash character in the path string. If the path contains no slash characters, it uses the host platform's rules. Or you may explicitly specify which rules to use by specifying the directorySeparator parameter in toPath. Pass "/" to get UNIX rules and "\" to get Windows rules.

Path Traversal

--------------

After the optional path root (like / on UNIX, like X:\ or \\ on Windows), the remainder of the path is a sequence of segments separated by / or \ characters. Segments satisfy these rules:

  • Segments are always non-empty.

  • If the segment is ., then the full path must be ..

  • For normalized paths, if the segment is .., then the path must be relative. All .. segments precede all other segments. In all cases, a segment .. cannot be the first segment of an absolute path.

The only path that ends with / is the file system root, /. The dot path . is a relative path that resolves to whichever path it is resolved against.

The name is the last segment in a path. It is typically a file or directory name, like README.md or Desktop. The name may be another special value:

  • The empty string is the name of the file system root path (full path /).

  • . is the name of the identity relative path (full path .).

  • .. is the name of a path consisting of only .. segments (such as ../../..).

Comparing Paths

---------------

Path implements Comparable, equals, and hashCode. If two paths are equal then they operate on the same file on the file system.

Note that the converse is not true: if two paths are non-equal, they may still resolve to the same file on the file system. Here are some of the ways non-equal paths resolve to the same file:

  • Case differences. The default file system on macOS is case-insensitive. The paths /Users/jesse/notes.txt and /USERS/JESSE/NOTES.TXT are non-equal but these paths resolve to the same file.

  • Mounting differences. Volumes may be mounted at multiple paths. On macOS, /Users/jesse/notes.txt and /Volumes/Macintosh HD/Users/jesse/notes.txt typically resolve to the same file. On Windows, C:\project\notes.txt and \\localhost\c$\project\notes.txt typically resolve to the same file.

  • Hard links. UNIX file systems permit multiple paths to refer for same file. The paths may be wildly different, like /Users/jesse/bruce_wayne.vcard and /Users/jesse/batman.vcard, but changes via either path are reflected in both.

  • Symlinks. Symlinks permit multiple paths and directories to refer to the same file. On macOS /tmp is symlinked to /private/tmp, so /tmp/notes.txt and /private/tmp/notes.txt resolve to the same file.

To test whether two paths refer to the same file, try FileSystem.canonicalize first. This follows symlinks and looks up the preserved casing for case-insensitive case-preserved paths. This method does not guarantee a unique result, however. For example, each hard link to a file may return its own canonical path.

Paths are sorted in case-sensitive order.

Sample Paths

------------

Path Parent Root Name Notes
`/` null `/` (empty) root
`/home/jesse/notes.txt` `/home/jesse` `/` `notes.txt` absolute path
`project/notes.txt` `project` null `notes.txt` relative path
`../../project/notes.txt` `../../project` null `notes.txt` relative path with traversal
`../../..` null null `..` relative path with traversal
`.` null null `.` current working directory
`C:\` null `C:\` (empty) volume root (Windows)
`C:\Windows\notepad.exe` `C:\Windows` `C:\` `notepad.exe` volume absolute path (Windows)
`\` null `\` (empty) absolute path (Windows)
`\Windows\notepad.exe` `\Windows` `\` `notepad.exe` absolute path (Windows)
`C:` null null (empty) volume-relative path (Windows)
`C:project\notes.txt` `C:project` null `notes.txt` volume-relative path (Windows)
`\\server` null `\\server` `server` UNC server (Windows)
`\\server\project\notes.txt` `\\server\project` `\\server` `notes.txt` UNC absolute path (Windows)
actual class Path : Comparable<Path>
actual class Path : Comparable<Path>

Types

Link copied to clipboard
expect object Companion
actual object Companion
actual object Companion

Properties

Link copied to clipboard
expect val isAbsolute: Boolean

This is true if root is not null.

actual val isAbsolute: Boolean
actual val isAbsolute: Boolean
Link copied to clipboard
expect val isRelative: Boolean

This is true if root is null.

actual val isRelative: Boolean
actual val isRelative: Boolean
Link copied to clipboard
expect val isRoot: Boolean

Returns true if this == this.root. That is, this is an absolute path with no parent.

actual val isRoot: Boolean
actual val isRoot: Boolean
Link copied to clipboard
expect val name: String
@get:JvmName(name = "name")
actual val name: String
actual val name: String
Link copied to clipboard
expect val nameBytes: ByteString
@get:JvmName(name = "nameBytes")
actual val nameBytes: ByteString
actual val nameBytes: ByteString
Link copied to clipboard
expect val parent: Path?

Returns the path immediately enclosing this path.

@get:JvmName(name = "parent")
actual val parent: Path?
actual val parent: Path?
Link copied to clipboard
expect val root: Path?

This is the root path if this is an absolute path, or null if it is a relative path. UNIX paths have a single root, /. Each volume on Windows is its own root, like C:\ and D:\. The path to the current volume \ is its own root. Windows UNC paths like \\server are also roots.

actual val root: Path?
actual val root: Path?
Link copied to clipboard
expect val segments: List<String>

The components of this path that are usually delimited by slashes. If the root is not null it precedes these segments. If this path is a root its segments list is empty.

actual val segments: List<String>
actual val segments: List<String>
Link copied to clipboard
Link copied to clipboard
expect val volumeLetter: Char?

This is the volume letter like "C" on Windows paths that starts with a volume letter. For example, on the path "C:\Windows" this returns "C". This property is null if this is not a Windows path, or if it doesn't have a volume letter.

@get:JvmName(name = "volumeLetter")
actual val volumeLetter: Char?
actual val volumeLetter: Char?

Functions

Link copied to clipboard
expect open operator override fun compareTo(other: Path): Int
actual open operator override fun compareTo(other: Path): Int
actual open operator override fun compareTo(other: Path): Int
Link copied to clipboard
expect operator fun div(child: String): Path
expect operator fun div(child: ByteString): Path
expect operator fun div(child: Path): Path

Returns a path that resolves child relative to this path. Note that the result isn't guaranteed to be normalized even if this and child are both normalized themselves.

@JvmName(name = "resolve")
actual operator fun div(child: String): Path
@JvmName(name = "resolve")
actual operator fun div(child: ByteString): Path
@JvmName(name = "resolve")
actual operator fun div(child: Path): Path
actual operator fun div(child: String): Path
actual operator fun div(child: ByteString): Path
actual operator fun div(child: Path): Path
Link copied to clipboard
expect open operator override fun equals(other: Any?): Boolean
actual open operator override fun equals(other: Any?): Boolean
actual open operator override fun equals(other: Any?): Boolean
Link copied to clipboard
expect open override fun hashCode(): Int
actual open override fun hashCode(): Int
actual open override fun hashCode(): Int
Link copied to clipboard
expect fun normalized(): Path

Returns the normalized version of this path. This has the same effect as this.toString().toPath(normalize = true).

actual fun normalized(): Path
actual fun normalized(): Path
Link copied to clipboard
expect fun relativeTo(other: Path): Path

Returns this path relative to other. This effectively inverts the resolve operator, /. For any two paths a and b that have the same root, a / (b.relativeTo(a)) is equal to b. If both paths don't use the same slash, the resolved path will use the slash of the other path.

actual fun relativeTo(other: Path): Path
actual fun relativeTo(other: Path): Path
Link copied to clipboard
expect fun resolve(child: String, normalize: Boolean = false): Path
expect fun resolve(child: ByteString, normalize: Boolean = false): Path
expect fun resolve(child: Path, normalize: Boolean = false): Path

Returns a path that resolves child relative to this path.

actual fun resolve(child: String, normalize: Boolean): Path
actual fun resolve(child: ByteString, normalize: Boolean): Path
actual fun resolve(child: Path, normalize: Boolean): Path
actual fun resolve(child: String, normalize: Boolean): Path
actual fun resolve(child: ByteString, normalize: Boolean): Path
actual fun resolve(child: Path, normalize: Boolean): Path
Link copied to clipboard
fun toFile(): File
Link copied to clipboard
Link copied to clipboard
expect open override fun toString(): String
actual open override fun toString(): String
actual open override fun toString(): String