1. Overview

In this quick tutorial, we’ll learn about various ways of writing content into a file using Kotlin extension methods – available in its standard library.

2. Kotlin File Extensions

Kotlin provides various ways of writing into a file in the form of extension methods for java.io.File.

We’ll use several of these to demonstrate different ways in which we can achieve this using Kotlin:

  • writeText – lets us write directly from a String
  • writeBytes – enables us to write directly from a ByteArray
  • printWriter – provides us with a PrintWriter
  • bufferedWriter – allows us to write using a BufferedWriter

Let’s discuss them in more detail.

3. Writing Directly

Writing directly into a File from a given source is the simplest strategy that we can expect using Kotlin extension methods.

3.1. writeText

Probably the most straightforward extension method, writeText takes the content as a String argument and writes it directly into the specified file. The given content is text encoded in UTF-8 (default) or any other specified charset:

File(fileName).writeText(fileContent)

This method internally delegates on writeBytes as described below. But first, it converts the given content into an array of bytes using the specified charset.

3.2. writeBytes

Likewise, we can use bytes as input. The method writeBytes takes a ByteArray as an argument and directly writes it into the specified file. This is useful when we have the contents as an array of bytes rather than plain text.

File(fileName).writeBytes(fileContentAsArray)

If the given file exists, it’s overwritten.

4. Writing into a File Using Writers

Kotlin also offers extension methods that provide us with a Java Writer instance.

4.1. printWriter

If we’d like to use a Java PrintWriter, Kotlin provides a printWriter function exactly for this purpose. With it, we can print formatted representations of objects to an OutputStream:

File(fileName).printWriter()

This method returns a new PrintWriter instance. Next, we can take advantage of the method use to handle it*:*

File(fileName).printWriter().use { out -> out.println(fileContent) }

With use, we can execute a function on the resource which gets closed after termination. The resource is closed irrespectively of whether the function executed successfully or threw an exception.

4.2. bufferedWriter

Likewise, Kotlin also provides a bufferedWriter function that provides us with a Java BufferedWriter.

Then, with it, we can write text to a character-output stream in a more efficient way.

File(fileName).bufferedWriter()

Similar to PrintWriter, this function returns a new BufferedWriter instance which, later, we can use to write the content of the file.

File(fileName).bufferedWriter().use { out -> out.write(fileContent) }

5. Appending Text to an Existing File

We’ve seen how to write content to a file. However, these operations overwrite the existing file content. Sometimes, we want to append new content to an existing file. So next, let’s look at how to append text to a file.

Similar to writeText() and writeBytes(), the appendText() and appendBytes() functions are also File extensions. As their names imply, these two extensions append new content to the file instead of overwriting the original file:

File(fileName).appendText(newContent)
File(fileName).appendBytes(newContentAsArray)

6. Conclusion

In this article, we explored different ways of writing and appending a file using Kotlin’s File extensions.

As always, the source code for this article and the relevant test cases are available over on GitHub.