Kotlin Program to Append Text to an Existing File

Kotlin Program to Append Text to an Existing File. Learn how to append text to file in Kotlin using Kotlin’s extension function, File.appendText() a single line of code, with an example program.

Kotlin Program to Append Text to an Existing File

Source Code

import java.io.IOException
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.StandardOpenOption

fun main(args: Array<String>) {

    val path = System.getProperty("user.dir") + "\src\test.txt"
    val text = "Added text"

    try {
        Files.write(Paths.get(path), text.toByteArray(), StandardOpenOption.APPEND)
    } catch (e: IOException) {
    }

}

Output