Different ways to write to a file in Kotlin:
Kotlin provides different ways to write to a file as direct methods and as extension methods. In this post, I will show you examples of different methods on how to write content to a file.
Method 1: By using File.writeText and File.appendText:
File.writeText is defined in java.io.File and using this method we can directly write one string to a file. Below is the definition of this method:
fun File.writeText(
str: String,
char: Charset = Charsets.UTF_8)
Here, str is the string that we are writing to the file and char is the Charset we are using. This is an optional value and by default it takes UTF-8.
For example:
package com.company
import java.io.File
fun main() {
val filePath = "/Users/cvc/IdeaProjects/KotlinSampleProgram/src/sample.txt"
val givenStr = "Hello World !!"
val file = File(filePath)
file.writeText(givenStr)
}
Here,
- filePath is the path of the file. Change it to your own file path
- givenStr is the string we are writing to the file.
- We created one File object with the file path and used writeText to write givenStr in that file.
It will write the below content to the file:
Hello World !!
Note that it replaces the content in the file. appendText is another method that can be used to append to the existing content of a file.
If I run the below code:
package com.company
import java.io.File
fun main() {
val filePath = "/Users/cvc/IdeaProjects/KotlinSampleProgram/src/sample.txt"
val givenStr = "Hello World !!"
val file = File(filePath)
file.writeText(givenStr)
file.appendText(givenStr)
}
The file will hold the below content:
Hello World !!Hello World !!
Method 2: By using File.writeBytes():
writeBytes is another method that can be used to write an array of bytes to a file. This method is defined as below:
fun File.writeBytes(array: ByteArray)
It takes a ByteArray and writes that byte array to the file.
Below program shows how we can use this method:
package com.company
import java.io.File
fun main() {
val filePath = "/Users/cvc/IdeaProjects/KotlinSampleProgram/src/sample.txt"
val givenStr = "Hello World !!"
val file = File(filePath)
file.writeBytes(givenStr.toByteArray())
}
Here,
- we are converting the string givenStr to a byte array using toByteArray() method and writing that in the file using writeBytes
It will add the below line to the file:
Hello World !!
It is not appending the content to the file. For that, we need to use appendBytes method.
Method 3: By using PrintWriter:
java.io.File PrintWriter can be used by printWriter() method provided in Kotlin. This method returns one PrintWriter instance. We can use use{} to handle it.
For example:
package com.company
import java.io.File
fun main() {
val filePath = "/Users/cvc/IdeaProjects/KotlinSampleProgram/src/sample.txt"
File(filePath).printWriter().use{
file ->
file.print('H')
file.print("ello")
file.println()
file.println("World")
}
}
It will print the below lines to the file:
Hello
World
Method 4: By using BufferedWriter:
Kotlin profiles another method called bufferedWriter that returns a new BufferedWriter object. We can use use{} on it to write different contents to a file.
For example:
package com.company
import java.io.File
fun main() {
val filePath = "/Users/cvc/IdeaProjects/KotlinSampleProgram/src/sample.txt"
File(filePath).bufferedWriter().use{
file ->
file.write("Hello ")
file.write("World")
}
}
It will print the below content to the file:
Hello World