Kotlin write a file with writeText

The writeText() is a Kotlin File extension function which writes text encoded using UTF-8 or other charsets to the file. If this file exists, it becomes overwritten.

Kotlin write a file with writeText

The example writes to a file with Kotlin writeText() extension function.
import java.io.File

fun main(args: Array<String>) {

    val fileName = "src/resources/myfile3.txt"
    val myfile = File(fileName)

    val content = "Today snow is falling."

    myfile.writeText(content)

    println("Writed to file")
}


Comments