CodeGym /Courses /Kotlin SELF /Writing text: writeText and appendText

Writing text: writeText and appendText

Kotlin SELF
Level 42, Lesson 3
Available

1. Introduction

To be honest, println() is like talking to your program through a window: convenient while you’re nearby and watching. But as soon as you need to save the result “for later” (a report, a log, a data export), the console becomes a poor archive: it gets cleared, lost, and in general is not obligated to be the source of truth. Writing to a file is a way to make your program’s output durable: you calculated it today, opened it tomorrow, and sent it to a colleague a week later.

In practice, writing text to a file is often needed even for the simplest console programs: save a report to "data/report.txt", keep an event log in "data/app.log", export a list of strings “as is” so that another tool (or you) can read it later. And most importantly, it’s very easy to do if you understand two methods: writeText() and appendText().

In this lecture we write for Kotlin/JVM and use java.io.File (this is normal and expected practice on the JVM).

2. writeText and appendText: overwrite vs append

writeText(...): overwrite the entire file

When you first see writeText("..."), your brain often thinks: “Oh, write text to a file, great.” And then the classic happens: “Why does my file become empty every time and contain only the last line?!” Because writeText overwrites the file completely. It behaves as if it says: “old contents — thanks, goodbye.”

This is very useful for reports, final results, and any files where you want to have the current version in full. For example: “generate an expense report” — you usually want the file to contain exactly one fresh report, not all the reports from the last 300 runs.

A mini example: wrote text, then read it back (reading was covered earlier; here it’s only for clarity).

import java.io.File

fun main() {
    val out = File("data/out.txt")
    out.parentFile?.mkdirs()

    out.writeText("Hello, file!\n")

    println(out.readText()) // Hello, file!
}

Pay attention to "\n": a newline does not “just happen on its own.” We’ll talk about this below, but it’s already useful to remember: if you write “in lines,” you most often add the newline yourself.

A slightly more “real-life” example: overwrite the file twice and see what remains.

import java.io.File

fun main() {
    val f = File("data/demo.txt")
    f.parentFile?.mkdirs()

    f.writeText("First\n")
    f.writeText("Second\n")

    println(f.readText()) // Second
}

Only "Second" will remain in the file. That’s expected. If you need to “add to what already exists,” that’s a different method.

appendText(...): append to the end

appendText() is a way to tell the file: “Don’t touch what’s already there; just add this to the end.” It’s perfect for logs, cumulative journals, simple action histories: “program started,” “user entered a command,” “export completed.”

It’s important to feel the difference at the level of intent:

  • A report is usually done via writeText(), because a report is “the current state in full.”
  • A log is usually done via appendText(), because a log is “a history of events over time.”

Mini example: “a log of two lines”:

import java.io.File

fun main() {
    val log = File("data/app.log")
    log.parentFile?.mkdirs()

    log.appendText("Started\n")
    log.appendText("Finished\n")

    println(log.readText())
    // Started
    // Finished
}

Here you can immediately see the second important idea: appendText does not add a newline automatically. It adds exactly the characters you gave it. If you forget "\n", the log will look as if it was written by a very hurried person in one breath.

Newlines: how not to glue entries together

Newlines are simple, but tricky. They are tricky because the human eye expects that “two entries” will be on two lines. But the computer doesn’t care: you told it “append text,” so it appended text. Where a “line” is — that’s your agreement with the format.

You have two basic approaches:

  • First: you always add "\n" at the end of each line entry when you do appendText(). This is the most direct, “honest” way.
  • Second: you build the multi-line text in advance (for example, via joinToString("\n") or via """...""".trimIndent()), and then do a single writeText(...).

A small table so you don’t get confused:

Scenario What we do Why
Write a final report in full
writeText(report + "\n")
easier to control the final format
Keep an event log
appendText(line + "\n")
each event is a separate line
Write a list of lines
writeText(lines.joinToString("\n") + "\n")
clear where the separators are

Example: we have a list of lines and we want to neatly save it as a “file of lines.”

import java.io.File

fun main() {
    val lines = listOf("milk", "bread", "coffee")
    val text = lines.joinToString(separator = "\n") + "\n"

    val file = File("data/list.txt")
    file.parentFile?.mkdirs()
    file.writeText(text)

    println(file.readText())
    // milk
    // bread
    // coffee
}

Why did I add + "\n" at the end? It’s not strictly required, but it’s a common habit: ending a text file with a newline. Then, if you later do appendText("new item\n"), you won’t get the last line “stuck” to the new entry.

3. Preparing the directory before writing

This is one of the most typical “first hits” when working with files: you build a path like "data/reports/report.txt", call writeText(...), and get an error. The reason is often not the file, but that the directory "data/reports" does not exist.

You can’t create a file in a folder that doesn’t exist. This sounds obvious, but a beginner’s brain for some reason is sure that the computer will “figure it out” and create the folders. No, it won’t: you didn’t tell it to.

The correct minimal pattern before writing:

  1. create a File for the target file
  2. create the parent directory via parentFile?.mkdirs()
  3. do writeText or appendText

Example:

import java.io.File

fun main() {
    val report = File("data/reports/today.txt")

    report.parentFile?.mkdirs()
    report.writeText("Report: OK\n")

    println(report.absolutePath) // (will print the absolute path on your machine)
}

There’s a neat nuance here: parentFile can be null if you write to a file without folders, for example File("out.txt"). That’s why we use the safe call ?..

Another useful point: mkdirs() creates a chain of directories. If you try mkdir() on "data/reports" but "data" doesn’t exist yet, mkdir() won’t handle it. In that sense, for “nested paths” mkdirs() is almost always more convenient.

4. Practical example: a report and a log for an expense tracker

So the topic doesn’t remain a set of separate commands, let’s weave file writing into a small fragment of our console application (let’s call it ExpenseTracker). Imagine that by this point in the course you already know how to store expenses in a collection and build a text report as a string. Now our task is to save that report to a file and, in parallel, keep a simple run log.

Let’s start with the model (minimal, so the example is self-contained):

data class Expense(val title: String, val amount: Int)

Now the function that builds the multi-line report text. We intentionally make it a separate function: the file is about writing, while the report is about formatting data. The separation “built text → wrote text” makes the program noticeably easier on the brain.

fun buildReport(expenses: List<Expense>): String {
    val total = expenses.sumOf { it.amount }
    val body = expenses.joinToString("\n") { "- ${it.title}: ${it.amount}" }
    return "Expenses:\n$body\nTotal: $total"
}

Note: here we form newlines inside the report string. That’s fine, because a report is exactly a text document.

Now—writing the report to a file via writeText(). And right away, in the same logic, we’ll prepare the directory.

import java.io.File

fun saveReport(reportText: String) {
    val file = File("data/reports/expenses.txt")
    file.parentFile?.mkdirs()
    file.writeText(reportText + "\n")
}

And now—a log via appendText(). Let each run add a line to "data/app.log".

import java.io.File

fun appendLog(message: String) {
    val log = File("data/app.log")
    log.parentFile?.mkdirs()
    log.appendText(message + "\n")
}

And a small main that shows the whole idea: created data, built the report, saved it, wrote a log entry.

import java.io.File

fun main() {
    val expenses = listOf(
        Expense("Coffee", 250),
        Expense("Lunch", 600)
    )

    val report = buildReport(expenses)
    saveReport(report)
    appendLog("Saved report with ${expenses.size} expenses")

    println("Done") // Done
}

Discipline matters here: main doesn’t turn into a “spaghetti of file operations.” It calls clear functions: buildReport, saveReport, appendLog. Inside the functions there is exactly one responsibility: prepare the path and write.

5. How to choose the method: a mini flow

When you write a real program, the choice between writeText and appendText can be almost mechanical. If you catch yourself thinking “I want the current version of the file, with exactly what was calculated now,” that’s writeText. If the thought is different: “I want to keep history, so new entries get added,” that’s appendText.

For visual memory, here’s a simple flowchart:

flowchart TD
    A["Need to write text to a file"] --> B{"Is this the final result (the file should reflect the current state)?"}
    B -->|Yes| C["writeText(...)"]
    B -->|No, it’s event history| D["appendText(...)"]
    C --> E["Don’t forget to prepare the folder: parentFile?.mkdirs()"]
    D --> E
    E --> F["Don’t forget the newline if writing in 'lines'"]

6. Common mistakes when writing text to a file

Mistake No. 1: using writeText where appendText is needed, and losing history.
The most painful case is logging. You want to see what happened over ten runs, but you see only the last event. The reason is almost always the same: out of habit, you used writeText and overwrote the file each time. For logs, journals, and cumulative lists you almost always want appendText.

Mistake No. 2: forgetting "\n" when appending and ending up with a “glued” file.
appendText("Started") and then appendText("Finished") will create "StartedFinished" in the file. The computer is not required to guess that you meant separate lines. If you write “line by line,” add the newline explicitly: appendText(message + "\n"). It seems like a small detail, but it’s exactly these details that later turn into “why is the log unreadable.”

Mistake No. 3: trying to write a file into a non-existent directory.
A path like "data/reports/report.txt" looks nice, but if the folder "data/reports" hasn’t been created, writing will fail. A good habit: before writeText/appendText always do file.parentFile?.mkdirs(). This is especially important when the path is built dynamically and you don’t control which subfolders will be needed.

Mistake No. 4: mixing “building the text” and “writing the text” into one huge chunk of code.
When you simultaneously do computations, formatting, string work, folder checks, and writing inside one block, the code becomes hard to read and easy to break. A much more resilient style is where you first obtain val reportText = ..., and then in one clear step call writeText(reportText). If the report format changes later, you won’t risk accidentally breaking the file-writing part.

Mistake No. 5: not understanding that appendText appends “as is,” including extra spaces and accidental blank lines.
Sometimes a line with extra spaces or an empty message ends up in the log, and then you “can’t figure out” where the junk came from. The reason is simple: you passed a string to appendText that you didn’t normalize. Even for logs, it’s useful to agree in advance: either you clean the string (trim()), or you consciously leave it as is, but then don’t be surprised by the consequences.

Mistake No. 6: writing to the same file from different parts of the program without a single rule.
If in one place you do writeText, and in another you do appendText to the same file, you’ll get chaotic behavior: sometimes the file “resets,” sometimes it suddenly grows. Discipline helps here: one file—one role. If it’s a report, it’s overwritten. If it’s a log, it’s appended. Mixing almost always leads to surprises.

1
Task
Kotlin SELF, level 42, lesson 3
Locked
Hello to a file
Hello to a file
1
Task
Kotlin SELF, level 42, lesson 3
Locked
Overwriting a Draft
Overwriting a Draft
1
Task
Kotlin SELF, level 42, lesson 3
Locked
Event log
Event log
1
Task
Kotlin SELF, level 42, lesson 3
Locked
List to a file
List to a file
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION