Extensions for java.io.File

Platform and version requirements: JVM (1.0)

appendBytes

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

fun File.appendBytes(array: ByteArray)
Platform and version requirements: JVM (1.0)

appendText

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

fun File.appendText(
    text: String, 
    charset: Charset = Charsets.UTF_8)
Platform and version requirements: JVM (1.0)

bufferedReader

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

fun File.bufferedReader(
    charset: Charset = Charsets.UTF_8, 
    bufferSize: Int = DEFAULT_BUFFER_SIZE
): BufferedReader
Platform and version requirements: JVM (1.0)

bufferedWriter

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

fun File.bufferedWriter(
    charset: Charset = Charsets.UTF_8, 
    bufferSize: Int = DEFAULT_BUFFER_SIZE
): BufferedWriter
Platform and version requirements: JVM (1.0)

copyRecursively

Copies this file with all its children to the specified destination target path. If some directories on the way to the destination are missing, then they will be created.

fun File.copyRecursively(
    target: File, 
    overwrite: Boolean = false, 
    onError: (File, IOException) -> OnErrorAction = { _, exception -> throw exception }
): Boolean
Platform and version requirements: JVM (1.0)

copyTo

Copies this file to the given target file.

fun File.copyTo(
    target: File, 
    overwrite: Boolean = false, 
    bufferSize: Int = DEFAULT_BUFFER_SIZE
): File
Platform and version requirements: JVM (1.0)

deleteRecursively

Delete this file with all its children. Note that if this operation fails then partial deletion may have taken place.

fun File.deleteRecursively(): Boolean
Platform and version requirements: JVM (1.0)

endsWith

Determines whether this file path ends with the path of other file.

fun File.endsWith(other: File): Boolean

Determines whether this file belongs to the same root as other and ends with all components of other in the same order. So if other has N components, last N components of this must be the same as in other. For relative other, this can belong to any root.

fun File.endsWith(other: String): Boolean
Platform and version requirements: JVM (1.0)

extension

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

val File.extension: String
Platform and version requirements: JVM (1.0)

forEachBlock

Reads file by byte blocks and calls action for each block read. Block has default size which is implementation-dependent. This functions passes the byte array and amount of bytes in the array to the action function.

fun File.forEachBlock(
    action: (buffer: ByteArray, bytesRead: Int) -> Unit)

Reads file by byte blocks and calls action for each block read. This functions passes the byte array and amount of bytes in the array to the action function.

fun File.forEachBlock(
    blockSize: Int, 
    action: (buffer: ByteArray, bytesRead: Int) -> Unit)
Platform and version requirements: JVM (1.0)

forEachLine

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

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

inputStream

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

fun File.inputStream(): FileInputStream
Platform and version requirements: JVM (1.0)

invariantSeparatorsPath

Returns path of this File using the invariant separator '/' to separate the names in the name sequence.

val File.invariantSeparatorsPath: String
Platform and version requirements: JVM (1.0)

isRooted

Determines whether this file has a root or it represents a relative path.

val File.isRooted: Boolean
Platform and version requirements: JVM (1.0)

nameWithoutExtension

Returns file's name without an extension.

val File.nameWithoutExtension: String
Platform and version requirements: JVM (1.0)

normalize

Removes all . and resolves all possible .. in this file name. For instance, File("/foo/./bar/gav/../baaz").normalize() is File("/foo/bar/baaz").

fun File.normalize(): File
Platform and version requirements: JVM (1.0)

outputStream

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

fun File.outputStream(): FileOutputStream
Platform and version requirements: JVM (1.0)

printWriter

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

fun File.printWriter(
    charset: Charset = Charsets.UTF_8
): PrintWriter
Platform and version requirements: JVM (1.0)

readBytes

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

fun File.readBytes(): ByteArray
Platform and version requirements: JVM (1.0)

reader

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

fun File.reader(
    charset: Charset = Charsets.UTF_8
): InputStreamReader
Platform and version requirements: JVM (1.0)

readLines

Reads the file content as a list of lines.

fun File.readLines(
    charset: Charset = Charsets.UTF_8
): List<String>
Platform and version requirements: JVM (1.0)

readText

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

fun File.readText(charset: Charset = Charsets.UTF_8): String
Platform and version requirements: JVM (1.0)

relativeTo

Calculates the relative path for this file from base file. Note that the base file is treated as a directory. If this file matches the base file, then a File with empty path will be returned.

fun File.relativeTo(base: File): File
Platform and version requirements: JVM (1.0)

relativeToOrNull

Calculates the relative path for this file from base file. Note that the base file is treated as a directory. If this file matches the base file, then a File with empty path will be returned.

fun File.relativeToOrNull(base: File): File?
Platform and version requirements: JVM (1.0)

relativeToOrSelf

Calculates the relative path for this file from base file. Note that the base file is treated as a directory. If this file matches the base file, then a File with empty path will be returned.

fun File.relativeToOrSelf(base: File): File
Platform and version requirements: JVM (1.0)

resolve

Adds relative file to this, considering this as a directory. If relative has a root, relative is returned back. For instance, File("/foo/bar").resolve(File("gav")) is File("/foo/bar/gav"). This function is complementary with relativeTo, so f.resolve(g.relativeTo(f)) == g should be always true except for different roots case.

fun File.resolve(relative: File): File

Adds relative name to this, considering this as a directory. If relative has a root, relative is returned back. For instance, File("/foo/bar").resolve("gav") is File("/foo/bar/gav").

fun File.resolve(relative: String): File
Platform and version requirements: JVM (1.0)

resolveSibling

Adds relative file to this parent directory. If relative has a root or this has no parent directory, relative is returned back. For instance, File("/foo/bar").resolveSibling(File("gav")) is File("/foo/gav").

fun File.resolveSibling(relative: File): File

Adds relative name to this parent directory. If relative has a root or this has no parent directory, relative is returned back. For instance, File("/foo/bar").resolveSibling("gav") is File("/foo/gav").

fun File.resolveSibling(relative: String): File
Platform and version requirements: JVM (1.0)

startsWith

Determines whether this file belongs to the same root as other and starts with all components of other in the same order. So if other has N components, first N components of this must be the same as in other.

fun File.startsWith(other: File): Boolean
fun File.startsWith(other: String): Boolean
Platform and version requirements: JVM (1.0)

toRelativeString

Calculates the relative path for this file from base file. Note that the base file is treated as a directory. If this file matches the base file, then an empty string will be returned.

fun File.toRelativeString(base: File): String
Platform and version requirements: JVM (1.0)

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> File.useLines(
    charset: Charset = Charsets.UTF_8, 
    block: (Sequence<String>) -> T
): T
Platform and version requirements: JVM (1.0)

walk

Gets a sequence for visiting this directory and all its content.

fun File.walk(
    direction: FileWalkDirection = FileWalkDirection.TOP_DOWN
): FileTreeWalk
Platform and version requirements: JVM (1.0)

walkBottomUp

Gets a sequence for visiting this directory and all its content in bottom-up order. Depth-first search is used and directories are visited after all their files.

fun File.walkBottomUp(): FileTreeWalk
Platform and version requirements: JVM (1.0)

walkTopDown

Gets a sequence for visiting this directory and all its content in top-down order. Depth-first search is used and directories are visited before all their files.

fun File.walkTopDown(): FileTreeWalk
Platform and version requirements: JVM (1.0)

writeBytes

Sets the content of this file as an array of bytes. If this file already exists, it becomes overwritten.

fun File.writeBytes(array: ByteArray)
Platform and version requirements: JVM (1.0)

writer

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

fun File.writer(
    charset: Charset = Charsets.UTF_8
): OutputStreamWriter
Platform and version requirements: JVM (1.0)

writeText

Sets the content of this file as text encoded using UTF-8 or specified charset. If this file exists, it becomes overwritten.

fun File.writeText(
    text: String, 
    charset: Charset = Charsets.UTF_8)

© 2010–2020 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/java.io.-file/index.html