DEV Community

loading...

Kotlin – How to write to file with Kotlin language

loizenai profile image loizenai ・1 min read

https://grokonez.com/kotlin/kotlin-write-file-kotlin-language

Kotlin – How to write to file with Kotlin language

This tutorial shows you how to write to File in Kotlin using PrintWriter, BufferedWriter or just File Kotlin extension functions.

I. Technology

  • Java 1.8
  • Kotlin 1.1.2

    II. Practice

    1. PrintWriter

    
    import java.io.File

fun main(args: Array) {

val outString: String = "Kotlin\nBe Kotlineer - Be Simple - Be Connective."
File("kotlin1.txt").printWriter().use { out -> out.println(outString) }
Enter fullscreen mode Exit fullscreen mode

}
Check Result:


Kotlin
Be Kotlineer - Be Simple - Be Connective.

2. BufferedWriter


import java.io.File

fun main(args: Array) {

    val outString: String = "Kotlin\nBe Kotlineer - Be Simple - Be Connective."
    File("kotlin2.txt").bufferedWriter().use { out -> out.write(outString) }
}

Check Result:

More at:

https://grokonez.com/kotlin/kotlin-write-file-kotlin-language

Kotlin – How to write to file with Kotlin language

Discussion (0)

Forem Open with the Forem app