Package kotlin.io.path

Convenient extensions for working with file system using java.nio.file.Path.

Types

ExperimentalPathApi
Link copied to clipboard
@Target(allowedTargets = [AnnotationTarget.CLASS, AnnotationTarget.ANNOTATION_CLASS, AnnotationTarget.PROPERTY, AnnotationTarget.FIELD, AnnotationTarget.LOCAL_VARIABLE, AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.CONSTRUCTOR, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER, AnnotationTarget.TYPEALIAS])
annotation class ExperimentalPathApi

This annotation marks the extensions and top-level functions for working with java.nio.file.Path considered experimental.

Since Kotlin

1.4

Functions

absolute
Link copied to clipboard
inline fun Path.absolute(): Path

Converts this possibly relative path to an absolute path.

Since Kotlin

1.5
absolutePathString
Link copied to clipboard
inline fun Path.absolutePathString(): String

Converts this possibly relative path to an absolute path and returns its string representation.

Since Kotlin

1.5
appendBytes
Link copied to clipboard
inline fun Path.appendBytes(array: ByteArray)

Appends an array of bytes to the content of this file.

Since Kotlin

1.5
appendLines
Link copied to clipboard
inline fun Path.appendLines(lines: Iterable<CharSequence>, charset: Charset = Charsets.UTF_8): Path

Appends the specified collection of char sequences lines to a file terminating each one with the platform's line separator.

Since Kotlin

1.5
inline fun Path.appendLines(lines: Sequence<CharSequence>, charset: Charset = Charsets.UTF_8): Path

Appends the specified sequence of char sequences lines to a file terminating each one with the platform's line separator.

Since Kotlin

1.5
appendText
Link copied to clipboard
fun Path.appendText(text: CharSequence, charset: Charset = Charsets.UTF_8)

Appends text to the content of this file using UTF-8 or the specified charset.

Since Kotlin

1.5
bufferedReader
Link copied to clipboard
inline fun Path.bufferedReader(charset: Charset = Charsets.UTF_8, bufferSize: Int = DEFAULT_BUFFER_SIZE, vararg options: OpenOption): BufferedReader

Returns a new BufferedReader for reading the content of this file.

Since Kotlin

1.5
bufferedWriter
Link copied to clipboard
inline fun Path.bufferedWriter(charset: Charset = Charsets.UTF_8, bufferSize: Int = DEFAULT_BUFFER_SIZE, vararg options: OpenOption): BufferedWriter

Returns a new BufferedWriter for writing the content of this file.

Since Kotlin

1.5
copyTo
Link copied to clipboard
inline fun Path.copyTo(target: Path, vararg options: CopyOption): Path
inline fun Path.copyTo(target: Path, overwrite: Boolean = false): Path

Copies a file or directory located by this path to the given target path.

Since Kotlin

1.5
createDirectories
Link copied to clipboard
inline fun Path.createDirectories(vararg attributes: FileAttribute<*>): Path

Creates a directory ensuring that all nonexistent parent directories exist by creating them first.

Since Kotlin

1.5
createDirectory
Link copied to clipboard
inline fun Path.createDirectory(vararg attributes: FileAttribute<*>): Path

Creates a new directory or throws an exception if there is already a file or directory located by this path.

Since Kotlin

1.5
createFile
Link copied to clipboard
inline fun Path.createFile(vararg attributes: FileAttribute<*>): Path

Creates a new and empty file specified by this path, failing if the file already exists.

Since Kotlin

1.5
createLinkPointingTo
Link copied to clipboard
inline fun Path.createLinkPointingTo(target: Path): Path

Creates a new link (directory entry) located by this path for the existing file target.

Since Kotlin

1.5
createSymbolicLinkPointingTo
Link copied to clipboard
inline fun Path.createSymbolicLinkPointingTo(target: Path, vararg attributes: FileAttribute<*>): Path

Creates a new symbolic link located by this path to the given target.

Since Kotlin

1.5
createTempDirectory
Link copied to clipboard
inline fun createTempDirectory(prefix: String? = null, vararg attributes: FileAttribute<*>): Path

Creates a new directory in the default temp directory, using the given prefix to generate its name.

Since Kotlin

1.5
fun createTempDirectory(directory: Path?, prefix: String? = null, vararg attributes: FileAttribute<*>): Path

Creates a new directory in the specified directory, using the given prefix to generate its name.

Since Kotlin

1.5
createTempFile
Link copied to clipboard
inline fun createTempFile(prefix: String? = null, suffix: String? = null, vararg attributes: FileAttribute<*>): Path

Creates an empty file in the default temp directory, using the given prefix and suffix to generate its name.

Since Kotlin

1.5
fun createTempFile(directory: Path?, prefix: String? = null, suffix: String? = null, vararg attributes: FileAttribute<*>): Path

Creates an empty file in the specified directory, using the given prefix and suffix to generate its name.

Since Kotlin

1.5
deleteExisting
Link copied to clipboard
inline fun Path.deleteExisting()

Deletes the existing file or empty directory specified by this path.

Since Kotlin

1.5
deleteIfExists
Link copied to clipboard
inline fun Path.deleteIfExists(): Boolean

Deletes the file or empty directory specified by this path if it exists.

Since Kotlin

1.5
div
Link copied to clipboard
inline operator fun Path.div(other: Path): Path

Resolves the given other path against this path.

Since Kotlin

1.5
inline operator fun Path.div(other: String): Path

Resolves the given other path string against this path.

Since Kotlin

1.5
exists
Link copied to clipboard
inline fun Path.exists(vararg options: LinkOption): Boolean

Checks if the file located by this path exists.

Since Kotlin

1.5
fileAttributesView
Link copied to clipboard
inline fun <V : FileAttributeView> Path.fileAttributesView(vararg options: LinkOption): V

Returns a file attributes view of a given type V or throws an UnsupportedOperationException if the requested attribute view type is not available..

Since Kotlin

1.5
fileAttributesViewOrNull
Link copied to clipboard
inline fun <V : FileAttributeView> Path.fileAttributesViewOrNull(vararg options: LinkOption): V?

Returns a file attributes view of a given type V or null if the requested attribute view type is not available.

Since Kotlin

1.5
fileSize
Link copied to clipboard
inline fun Path.fileSize(): Long

Returns the size of a regular file as a Long value of bytes or throws an exception if the file doesn't exist.

Since Kotlin

1.5
fileStore
Link copied to clipboard
inline fun Path.fileStore(): FileStore

Returns the FileStore representing the file store where a file is located.

Since Kotlin

1.5
forEachDirectoryEntry
Link copied to clipboard
inline fun Path.forEachDirectoryEntry(glob: String = "*", action: (Path) -> Unit)

Performs the given action on each entry in this directory optionally filtered by matching against the specified glob pattern.

Since Kotlin

1.5
forEachLine
Link copied to clipboard
inline fun Path.forEachLine(charset: Charset = Charsets.UTF_8, action: (line: String) -> Unit)

Reads this file line by line using the specified charset and calls action for each line. Default charset is UTF-8.

Since Kotlin

1.5
getAttribute
Link copied to clipboard
inline fun Path.getAttribute(attribute: String, vararg options: LinkOption): Any?

Reads the value of a file attribute.

Since Kotlin

1.5
getLastModifiedTime
Link copied to clipboard
inline fun Path.getLastModifiedTime(vararg options: LinkOption): FileTime

Returns the last modified time of the file located by this path.

Since Kotlin

1.5
getOwner
Link copied to clipboard
inline fun Path.getOwner(vararg options: LinkOption): UserPrincipal?

Returns the owner of a file.

Since Kotlin

1.5
getPosixFilePermissions
Link copied to clipboard
inline fun Path.getPosixFilePermissions(vararg options: LinkOption): Set<PosixFilePermission>

Returns the POSIX file permissions of the file located by this path.

Since Kotlin

1.5
inputStream
Link copied to clipboard
inline fun Path.inputStream(vararg options: OpenOption): InputStream

Constructs a new InputStream of this file and returns it as a result.

Since Kotlin

1.5
isDirectory
Link copied to clipboard
inline fun Path.isDirectory(vararg options: LinkOption): Boolean

Checks if the file located by this path is a directory.

Since Kotlin

1.5
isExecutable
Link copied to clipboard
inline fun Path.isExecutable(): Boolean

Checks if the file located by this path exists and is executable.

Since Kotlin

1.5
isHidden
Link copied to clipboard
inline fun Path.isHidden(): Boolean

Checks if the file located by this path is considered hidden.

Since Kotlin

1.5
isReadable
Link copied to clipboard
inline fun Path.isReadable(): Boolean

Checks if the file located by this path exists and is readable.

Since Kotlin

1.5
isRegularFile
Link copied to clipboard
inline fun Path.isRegularFile(vararg options: LinkOption): Boolean

Checks if the file located by this path is a regular file.

Since Kotlin

1.5
isSameFileAs
Link copied to clipboard
inline fun Path.isSameFileAs(other: Path): Boolean

Checks if the file located by this path points to the same file or directory as other.

Since Kotlin

1.5
isSymbolicLink
Link copied to clipboard
inline fun Path.isSymbolicLink(): Boolean

Checks if the file located by this path exists and is a symbolic link.

Since Kotlin

1.5
isWritable
Link copied to clipboard
inline fun Path.isWritable(): Boolean

Checks if the file located by this path exists and is writable.

Since Kotlin

1.5
listDirectoryEntries
Link copied to clipboard
fun Path.listDirectoryEntries(glob: String = "*"): List<Path>

Returns a list of the entries in this directory optionally filtered by matching against the specified glob pattern.

Since Kotlin

1.5
moveTo
Link copied to clipboard
inline fun Path.moveTo(target: Path, vararg options: CopyOption): Path
inline fun Path.moveTo(target: Path, overwrite: Boolean = false): Path

Moves or renames the file located by this path to the target path.

Since Kotlin

1.5
notExists
Link copied to clipboard
inline fun Path.notExists(vararg options: LinkOption): Boolean

Checks if the file located by this path does not exist.

Since Kotlin

1.5
outputStream
Link copied to clipboard
inline fun Path.outputStream(vararg options: OpenOption): OutputStream

Constructs a new OutputStream of this file and returns it as a result.

Since Kotlin

1.5
Path
Link copied to clipboard
inline fun Path(path: String): Path

Converts the provided path string to a Path object of the default filesystem.

Since Kotlin

1.5
inline fun Path(base: String, vararg subpaths: String): Path

Converts the name sequence specified with the base path string and a number of subpaths additional names to a Path object of the default filesystem.

Since Kotlin

1.5
readAttributes
Link copied to clipboard
inline fun <A : BasicFileAttributes> Path.readAttributes(vararg options: LinkOption): A

Reads a file's attributes of the specified type A in bulk.

Since Kotlin

1.5
inline fun Path.readAttributes(attributes: String, vararg options: LinkOption): Map<String, Any?>

Reads the specified list of attributes of a file in bulk.

Since Kotlin

1.5
readBytes
Link copied to clipboard
inline fun Path.readBytes(): ByteArray

Gets the entire content of this file as a byte array.

Since Kotlin

1.5
reader
Link copied to clipboard
inline fun Path.reader(charset: Charset = Charsets.UTF_8, vararg options: OpenOption): InputStreamReader

Returns a new InputStreamReader for reading the content of this file.

Since Kotlin

1.5
readLines
Link copied to clipboard
inline fun Path.readLines(charset: Charset = Charsets.UTF_8): List<String>

Reads the file content as a list of lines.

Since Kotlin

1.5
readSymbolicLink
Link copied to clipboard
inline fun Path.readSymbolicLink(): Path

Reads the target of a symbolic link located by this path.

Since Kotlin

1.5
readText
Link copied to clipboard
fun Path.readText(charset: Charset = Charsets.UTF_8): String

Gets the entire content of this file as a String using UTF-8 or the specified charset.

Since Kotlin

1.5
relativeTo
Link copied to clipboard
fun Path.relativeTo(base: Path): Path

Calculates the relative path for this path from a base path.

Since Kotlin

1.5
relativeToOrNull
Link copied to clipboard
fun Path.relativeToOrNull(base: Path): Path?

Calculates the relative path for this path from a base path.

Since Kotlin

1.5
relativeToOrSelf
Link copied to clipboard
fun Path.relativeToOrSelf(base: Path): Path

Calculates the relative path for this path from a base path.

Since Kotlin

1.5
setAttribute
Link copied to clipboard
inline fun Path.setAttribute(attribute: String, value: Any?, vararg options: LinkOption): Path

Sets the value of a file attribute.

Since Kotlin

1.5
setLastModifiedTime
Link copied to clipboard
inline fun Path.setLastModifiedTime(value: FileTime): Path

Sets the last modified time attribute for the file located by this path.

Since Kotlin

1.5
setOwner
Link copied to clipboard
inline fun Path.setOwner(value: UserPrincipal): Path

Sets the file owner to the specified value.

Since Kotlin

1.5
setPosixFilePermissions
Link copied to clipboard
inline fun Path.setPosixFilePermissions(value: Set<PosixFilePermission>): Path

Sets the POSIX file permissions for the file located by this path.

Since Kotlin

1.5
toPath
Link copied to clipboard
inline fun URI.toPath(): Path

Converts this URI to a Path object.

Since Kotlin

1.5
useDirectoryEntries
Link copied to clipboard
inline fun <T> Path.useDirectoryEntries(glob: String = "*", block: (Sequence<Path>) -> T): T

Calls the block callback with a sequence of all entries in this directory optionally filtered by matching against the specified glob pattern.

Since Kotlin

1.5
useLines
Link copied to clipboard
inline fun <T> Path.useLines(charset: Charset = Charsets.UTF_8, block: (Sequence<String>) -> T): T

Calls the block callback giving it a sequence of all the lines in this file and closes the reader once the processing is complete.

Since Kotlin

1.5
writeBytes
Link copied to clipboard
inline fun Path.writeBytes(array: ByteArray, vararg options: OpenOption)

Writes an array of bytes to this file.

Since Kotlin

1.5
writeLines
Link copied to clipboard
inline fun Path.writeLines(lines: Iterable<CharSequence>, charset: Charset = Charsets.UTF_8, vararg options: OpenOption): Path

Write the specified collection of char sequences lines to a file terminating each one with the platform's line separator.

Since Kotlin

1.5
inline fun Path.writeLines(lines: Sequence<CharSequence>, charset: Charset = Charsets.UTF_8, vararg options: OpenOption): Path

Write the specified sequence of char sequences lines to a file terminating each one with the platform's line separator.

Since Kotlin

1.5
writer
Link copied to clipboard
inline fun Path.writer(charset: Charset = Charsets.UTF_8, vararg options: OpenOption): OutputStreamWriter

Returns a new OutputStreamWriter for writing the content of this file.

Since Kotlin

1.5
writeText
Link copied to clipboard
fun Path.writeText(text: CharSequence, charset: Charset = Charsets.UTF_8, vararg options: OpenOption)

Sets the content of this file as text encoded using UTF-8 or the specified charset.

Since Kotlin

1.5

Properties

extension
Link copied to clipboard
val Path.extension: String

Returns the extension of this path (not including the dot), or an empty string if it doesn't have one.

invariantSeparatorsPath
Link copied to clipboard
val Path.invariantSeparatorsPath: String
invariantSeparatorsPathString
Link copied to clipboard
val Path.invariantSeparatorsPathString: String

Returns the string representation of this path using the invariant separator '/' to separate names in the path.

name
Link copied to clipboard
val Path.name: String

Returns the name of the file or directory denoted by this path as a string, or an empty string if this path has zero path elements.

nameWithoutExtension
Link copied to clipboard
val Path.nameWithoutExtension: String

Returns the name of this file or directory without an extension, or an empty string if this path has zero path elements.

pathString
Link copied to clipboard
val Path.pathString: String

Returns the string representation of this path.