Kotlin write file with PrintWriter

PrintWriter prints formatted representations of objects to a text-output stream.

Kotlin write a file with PrintWriter

The example writes two lines to a file with PrintWriter.
import java.io.File

fun main(args: Array<String>) {

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

    myfile.printWriter().use { out ->

        out.println("First line")
        out.println("Second line")
    }

    println("Writed to file")
}
We write to src/resources/myfile.txt file:
val fileName = "src/resources/myfile.txt"

Comments