Extensions for java.nio.file.Path

Platform and version requirements: JVM (1.5), JRE7 (1.5)

absolute

Converts this possibly relative path to an absolute path.

fun Path.absolute(): Path
Platform and version requirements: JVM (1.5), JRE7 (1.5)

absolutePathString

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

fun Path.absolutePathString(): String
Platform and version requirements: JVM (1.5), JRE7 (1.5)

appendBytes

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

fun Path.appendBytes(array: ByteArray)
Platform and version requirements: JVM (1.5), JRE7 (1.5)

appendLines

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

fun Path.appendLines(
    lines: Iterable<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.

fun Path.appendLines(
    lines: Sequence<CharSequence>, 
    charset: Charset = Charsets.UTF_8
): Path
Platform and version requirements: JVM (1.5), JRE7 (1.5)

appendText

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

fun Path.appendText(
    text: CharSequence, 
    charset: Charset = Charsets.UTF_8)
Platform and version requirements: JVM (1.5), JRE7 (1.5)

bufferedReader

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

fun Path.bufferedReader(
    charset: Charset = Charsets.UTF_8, 
    bufferSize: Int = DEFAULT_BUFFER_SIZE, 
    vararg options: OpenOption
): BufferedReader
Platform and version requirements: JVM (1.5), JRE7 (1.5)

bufferedWriter

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

fun Path.bufferedWriter(
    charset: Charset = Charsets.UTF_8, 
    bufferSize: Int = DEFAULT_BUFFER_SIZE, 
    vararg options: OpenOption
): BufferedWriter
Platform and version requirements: JVM (1.5), JRE7 (1.5)

copyTo

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

fun Path.copyTo(
    target: Path, 
    overwrite: Boolean = false
): Path
fun Path.copyTo(
    target: Path, 
    vararg options: CopyOption
): Path
Platform and version requirements: JVM (1.5), JRE7 (1.5)

createDirectories

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

fun Path.createDirectories(
    vararg attributes: FileAttribute<*>
): Path
Platform and version requirements: JVM (1.5), JRE7 (1.5)

createDirectory

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

fun Path.createDirectory(
    vararg attributes: FileAttribute<*>
): Path
Platform and version requirements: JVM (1.5), JRE7 (1.5)

createFile

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

fun Path.createFile(
    vararg attributes: FileAttribute<*>
): Path
Platform and version requirements: JVM (1.5), JRE7 (1.5)

createLinkPointingTo

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

fun Path.createLinkPointingTo(target: Path): Path
Platform and version requirements: JVM (1.5), JRE7 (1.5)

createSymbolicLinkPointingTo

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

fun Path.createSymbolicLinkPointingTo(
    target: Path, 
    vararg attributes: FileAttribute<*>
): Path
Platform and version requirements: JVM (1.5), JRE7 (1.5)

deleteExisting

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

fun Path.deleteExisting()
Platform and version requirements: JVM (1.5), JRE7 (1.5)

deleteIfExists

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

fun Path.deleteIfExists(): Boolean
Platform and version requirements: JVM (1.5), JRE7 (1.5)

div

Resolves the given other path against this path.

operator fun Path.div(other: Path): Path

Resolves the given other path string against this path.

operator fun Path.div(other: String): Path
Platform and version requirements: JVM (1.5), JRE7 (1.5)

exists

Checks if the file located by this path exists.

fun Path.exists(vararg options: LinkOption): Boolean
Platform and version requirements: JVM (1.5), JRE7 (1.5)

extension

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

val Path.extension: String
Platform and version requirements: JVM (1.5), JRE7 (1.5)

fileAttributesView

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

fun <V : FileAttributeView> Path.fileAttributesView(
    vararg options: LinkOption
): V
Platform and version requirements: JVM (1.5), JRE7 (1.5)

fileAttributesViewOrNull

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

fun <V : FileAttributeView> Path.fileAttributesViewOrNull(
    vararg options: LinkOption
): V?
Platform and version requirements: JVM (1.5), JRE7 (1.5)

fileSize

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

fun Path.fileSize(): Long
Platform and version requirements: JVM (1.5), JRE7 (1.5)

fileStore

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

fun Path.fileStore(): FileStore
Platform and version requirements: JVM (1.5), JRE7 (1.5)

forEachDirectoryEntry

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

fun Path.forEachDirectoryEntry(
    glob: String = "*", 
    action: (Path) -> Unit)
Platform and version requirements: JVM (1.5), JRE7 (1.5)

forEachLine

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

fun Path.forEachLine(
    charset: Charset = Charsets.UTF_8, 
    action: (line: String) -> Unit)
Platform and version requirements: JVM (1.5), JRE7 (1.5)

getAttribute

Reads the value of a file attribute.

fun Path.getAttribute(
    attribute: String, 
    vararg options: LinkOption
): Any?
Platform and version requirements: JVM (1.5), JRE7 (1.5)

getLastModifiedTime

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

fun Path.getLastModifiedTime(
    vararg options: LinkOption
): FileTime
Platform and version requirements: JVM (1.5), JRE7 (1.5)

getOwner

Returns the owner of a file.

fun Path.getOwner(vararg options: LinkOption): UserPrincipal?
Platform and version requirements: JVM (1.5), JRE7 (1.5)

getPosixFilePermissions

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

fun Path.getPosixFilePermissions(
    vararg options: LinkOption
): Set<PosixFilePermission>
Platform and version requirements: JVM (1.5), JRE7 (1.5)

inputStream

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

fun Path.inputStream(vararg options: OpenOption): InputStream
Platform and version requirements: JVM (1.4), JRE7 (1.4)

invariantSeparatorsPath

val Path.invariantSeparatorsPath: String
Platform and version requirements: JVM (1.5), JRE7 (1.5)

invariantSeparatorsPathString

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

val Path.invariantSeparatorsPathString: String
Platform and version requirements: JVM (1.5), JRE7 (1.5)

isDirectory

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

fun Path.isDirectory(vararg options: LinkOption): Boolean
Platform and version requirements: JVM (1.5), JRE7 (1.5)

isExecutable

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

fun Path.isExecutable(): Boolean
Platform and version requirements: JVM (1.5), JRE7 (1.5)

isHidden

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

fun Path.isHidden(): Boolean
Platform and version requirements: JVM (1.5), JRE7 (1.5)

isReadable

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

fun Path.isReadable(): Boolean
Platform and version requirements: JVM (1.5), JRE7 (1.5)

isRegularFile

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

fun Path.isRegularFile(vararg options: LinkOption): Boolean
Platform and version requirements: JVM (1.5), JRE7 (1.5)

isSameFileAs

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

fun Path.isSameFileAs(other: Path): Boolean
Platform and version requirements: JVM (1.5), JRE7 (1.5)

isSymbolicLink

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

fun Path.isSymbolicLink(): Boolean
Platform and version requirements: JVM (1.5), JRE7 (1.5)

isWritable

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

fun Path.isWritable(): Boolean
Platform and version requirements: JVM (1.5), JRE7 (1.5)

listDirectoryEntries

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

fun Path.listDirectoryEntries(glob: String = "*"): List<Path>
Platform and version requirements: JVM (1.5), JRE7 (1.5)

moveTo

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

fun Path.moveTo(
    target: Path, 
    vararg options: CopyOption
): Path
fun Path.moveTo(
    target: Path, 
    overwrite: Boolean = false
): Path
Platform and version requirements: JVM (1.5), JRE7 (1.5)

name

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.

val Path.name: String
Platform and version requirements: JVM (1.5), JRE7 (1.5)

nameWithoutExtension

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

val Path.nameWithoutExtension: String
Platform and version requirements: JVM (1.5), JRE7 (1.5)

notExists

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

fun Path.notExists(vararg options: LinkOption): Boolean
Platform and version requirements: JVM (1.5), JRE7 (1.5)

outputStream

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

fun Path.outputStream(
    vararg options: OpenOption
): OutputStream
Platform and version requirements: JVM (1.5), JRE7 (1.5)

pathString

Returns the string representation of this path.

val Path.pathString: String
Platform and version requirements: JVM (1.5), JRE7 (1.5)

readAttributes

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

fun <A : BasicFileAttributes> Path.readAttributes(
    vararg options: LinkOption
): A

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

fun Path.readAttributes(
    attributes: String, 
    vararg options: LinkOption
): Map<String, Any?>
Platform and version requirements: JVM (1.5), JRE7 (1.5)

readBytes

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

fun Path.readBytes(): ByteArray
Platform and version requirements: JVM (1.5), JRE7 (1.5)

reader

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

fun Path.reader(
    charset: Charset = Charsets.UTF_8, 
    vararg options: OpenOption
): InputStreamReader
Platform and version requirements: JVM (1.5), JRE7 (1.5)

readLines

Reads the file content as a list of lines.

fun Path.readLines(
    charset: Charset = Charsets.UTF_8
): List<String>
Platform and version requirements: JVM (1.5), JRE7 (1.5)

readSymbolicLink

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

fun Path.readSymbolicLink(): Path
Platform and version requirements: JVM (1.5), JRE7 (1.5)

readText

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

fun Path.readText(charset: Charset = Charsets.UTF_8): String
Platform and version requirements: JVM (1.5), JRE7 (1.5)

relativeTo

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

fun Path.relativeTo(base: Path): Path
Platform and version requirements: JVM (1.5), JRE7 (1.5)

relativeToOrNull

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

fun Path.relativeToOrNull(base: Path): Path?
Platform and version requirements: JVM (1.5), JRE7 (1.5)

relativeToOrSelf

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

fun Path.relativeToOrSelf(base: Path): Path
Platform and version requirements: JVM (1.5), JRE7 (1.5)

setAttribute

Sets the value of a file attribute.

fun Path.setAttribute(
    attribute: String, 
    value: Any?, 
    vararg options: LinkOption
): Path
Platform and version requirements: JVM (1.5), JRE7 (1.5)

setLastModifiedTime

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

fun Path.setLastModifiedTime(value: FileTime): Path
Platform and version requirements: JVM (1.5), JRE7 (1.5)

setOwner

Sets the file owner to the specified value.

fun Path.setOwner(value: UserPrincipal): Path
Platform and version requirements: JVM (1.5), JRE7 (1.5)

setPosixFilePermissions

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

fun Path.setPosixFilePermissions(
    value: Set<PosixFilePermission>
): Path
Platform and version requirements: JVM (1.5), JRE7 (1.5)

useDirectoryEntries

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

fun <T> Path.useDirectoryEntries(
    glob: String = "*", 
    block: (Sequence<Path>) -> T
): T
Platform and version requirements: JVM (1.5), JRE7 (1.5)

useLines

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

fun <T> Path.useLines(
    charset: Charset = Charsets.UTF_8, 
    block: (Sequence<String>) -> T
): T
Platform and version requirements: JVM (1.5), JRE7 (1.5)

writeBytes

Writes an array of bytes to this file.

fun Path.writeBytes(
    array: ByteArray, 
    vararg options: OpenOption)
Platform and version requirements: JVM (1.5), JRE7 (1.5)

writeLines

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

fun Path.writeLines(
    lines: Iterable<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.

fun Path.writeLines(
    lines: Sequence<CharSequence>, 
    charset: Charset = Charsets.UTF_8, 
    vararg options: OpenOption
): Path
Platform and version requirements: JVM (1.5), JRE7 (1.5)

writer

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

fun Path.writer(
    charset: Charset = Charsets.UTF_8, 
    vararg options: OpenOption
): OutputStreamWriter
Platform and version requirements: JVM (1.5), JRE7 (1.5)

writeText

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

fun Path.writeText(
    text: CharSequence, 
    charset: Charset = Charsets.UTF_8, 
    vararg options: OpenOption)

© 2010–2021 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io.path/java.nio.file.-path/