Kotlin:
Writing a text file
How to:
Kotlin provides a straightforward approach for writing to files, leveraging the standard library without needing additional third-party libraries. Here is a simple example:
import java.io.File
fun main() {
val textToWrite = "Hello, Kotlin file writing!"
File("example.txt").writeText(textToWrite)
}
This code snippet creates a file named “example.txt” in the project’s root directory and writes the string Hello, Kotlin file writing!
into it. If the file already exists, it will be overwritten.
For more controlled appending to a file or writing larger amounts of data, you can use appendText
or bufferedWriter()
:
import java.io.File
fun appendToFile() {
val moreText = "Appending more text."
File("example.txt").appendText(moreText)
}
fun writeWithBufferedWriter() {
val largeText = "Large amounts of text...\nOn multiple lines."
File("output.txt").bufferedWriter().use { out ->
out.write(largeText)
}
}
fun main() {
appendToFile() // Appends text to the existing file
writeWithBufferedWriter() // Writes large text data efficiently
}
In the appendToFile
function, we’re adding more text to “example.txt” without overwriting its current content. The writeWithBufferedWriter
function showcases an efficient way to write large amounts of text or data, especially useful for minimizing I/O operations when dealing with multiple lines or large files.
These examples cover basic operations for writing text files in Kotlin, showcasing the simplicity and power of Kotlin’s standard library for file I/O operations.