Write to a file in Kotlin
This article covers different ways to write data to a text file in Kotlin. The solution should truncate the file before writing or create a new file if it doesn’t exist.
1. Using File.writeText() function
The standard approach to set the file’s content is with the File.writeText() function. The data is encoded using the default UTF-8 or the specified charset.
|
1 2 3 4 5 6 7 8 |
import java.io.File fun main() { val file = File("foo.out") val text = "Some log…" file.writeText(text, Charsets.UTF_8) } |
2. Using PrintWriter
The PrintWriter is the recommended class that requires writing characters rather than bytes.
|
1 2 3 4 5 6 7 8 9 |
import java.io.File import java.io.PrintWriter fun main() { val file = File("foo.out") val text = "Some log…" PrintWriter(file, Charsets.UTF_8).use { it.print(text) } } |
3. Using Files.newBufferedWriter() function
The Files.newBufferedWriter() function returns a BufferedWriter that may be used to write text to the file efficiently.
|
1 2 3 4 5 6 7 8 9 |
import java.nio.file.Files import java.nio.file.Paths fun main() { val path = Paths.get("foo.out") val text = "Some log…" Files.newBufferedWriter(path, Charsets.UTF_8).use { it.write(text) } } |
4. Using OutputStreamWriter
Since FileOutputStream is meant for writing streams of bytes, you can construct an OutputStreamWriter for writing streams of characters. To improve efficiency, it is advisable to wrap a BufferedWriter around OutputStreamWriter.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.io.BufferedWriter import java.io.File import java.io.FileOutputStream import java.io.OutputStreamWriter fun main() { val file = File("foo.out") val text = "Some text" FileOutputStream(file).use { fos -> OutputStreamWriter(fos, Charsets.UTF_8).use { osw -> BufferedWriter(osw).use { bf -> bf.write(text) } } } } |
5. Using FileWriter
The FileWriter is a convenience class for writing streams of characters using default character encoding. We can use this as:
|
1 2 3 4 5 6 7 8 9 |
import java.io.File import java.io.FileWriter fun main() { val file = File("foo.out") val text = "Some log…" FileWriter(file).use { it.write(text) } } |
6. Using File.bufferedWriter() function
It is advisable to wrap BufferedWriter around file writer as its write operations are very costly. We can do this with the File.bufferedWriter() function, which returns a new BufferedWriter for writing this file’s content.
|
1 2 3 4 5 6 7 8 |
import java.io.File fun main() { val file = File("foo.out") val text = "Some log…" file.bufferedWriter().use { bw -> bw.write(text) } } |
7. Using PrintStream
A PrintStream can be used to add functionality to an output stream. But all characters printed by a PrintStream are converted into bytes using the platform’s default character encoding.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.io.File import java.io.FileOutputStream import java.io.PrintStream fun main() { val file = File("foo.out") val text = "Some log…" FileOutputStream(file).use { fos -> PrintStream(fos).use { out -> out.print(text) } } } |
That’s all about writing to a file in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)