Extensions for java.io.File

appendBytes

fun File.appendBytes(array: ByteArray)

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

appendText

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

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

bufferedReader

fun File.bufferedReader(
    charset: Charset = Charsets.UTF_8,
    bufferSize: Int = DEFAULT_BUFFER_SIZE
): BufferedReader

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

bufferedWriter

fun File.bufferedWriter(
    charset: Charset = Charsets.UTF_8,
    bufferSize: Int = DEFAULT_BUFFER_SIZE
): BufferedWriter

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

copyRecursively

fun File.copyRecursively(
    target: File,
    overwrite: Boolean = false,
    onError: (File, IOException) -> OnErrorAction = { _, exception -> throw exception }
): Boolean

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.

copyTo

fun File.copyTo(
    target: File,
    overwrite: Boolean = false,
    bufferSize: Int = DEFAULT_BUFFER_SIZE
): File

Copies this file to the given target file.

deleteRecursively

fun File.deleteRecursively(): Boolean

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

endsWith

fun File.endsWith(other: File): Boolean

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

fun File.endsWith(other: String): 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.

extension

val File.extension: String

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

forEachBlock

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

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(
    blockSize: Int,
    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.

forEachLine

fun File.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.

inputStream

fun File.inputStream(): FileInputStream

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

invariantSeparatorsPath

val File.invariantSeparatorsPath: String

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

isRooted

val File.isRooted: Boolean

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

nameWithoutExtension

val File.nameWithoutExtension: String

Returns file's name without an extension.

normalize

fun File.normalize(): File

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

outputStream

fun File.outputStream(): FileOutputStream

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

printWriter

fun File.printWriter(
    charset: Charset = Charsets.UTF_8
): PrintWriter

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

readBytes

fun File.readBytes(): ByteArray

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

readLines

fun File.readLines(
    charset: Charset = Charsets.UTF_8
): List<String>

Reads the file content as a list of lines.

readText

fun File.readText(charset: Charset = Charsets.UTF_8): String

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

reader

fun File.reader(
    charset: Charset = Charsets.UTF_8
): InputStreamReader

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

relativeTo

fun File.relativeTo(base: File): File

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.

relativeToOrNull

fun File.relativeToOrNull(base: File): File?

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.

relativeToOrSelf

fun File.relativeToOrSelf(base: File): File

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.

resolve

fun File.resolve(relative: File): File

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: String): 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").

resolveSibling

fun File.resolveSibling(relative: File): File

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: String): 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").

startsWith

fun File.startsWith(other: File): Boolean
fun File.startsWith(other: String): Boolean

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.

toRelativeString

fun File.toRelativeString(base: File): String

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.

useLines

fun <T> File.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.

walk

fun File.walk(
    direction: FileWalkDirection = FileWalkDirection.TOP_DOWN
): FileTreeWalk

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

walkBottomUp

fun File.walkBottomUp(): FileTreeWalk

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.

walkTopDown

fun File.walkTopDown(): FileTreeWalk

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.

writeBytes

fun File.writeBytes(array: ByteArray)

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

writeText

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

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

writer

fun File.writer(
    charset: Charset = Charsets.UTF_8
): OutputStreamWriter

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