Something is wrong with my code that works with File IO.
fun main() {
val strs = listOf(
"line1",
"line2"
)
val writer = File("src/main/resources/logging/temp.txt")
.writer()
strs.forEach { writer.appendLine(it) }
writer.close()
}
The
temp.txt
file is empty when I run this program.
After that the contents of the
temp.txt
file looks something like this,
line1
line2
This is a perfectly reasonable output. However, things go wrong when I run a slightly modified version of the code for the second time.
fun main() {
val strs = listOf(
"line3",
"line4"
)
val writer = File("src/main/resources/logging/temp.txt")
.writer()
strs.forEach { writer.appendLine(it) }
writer.close()
}
This is the new version. But this time the
temp.txt
file wasn't empty. It had the output of the first program saved into it.
However, after running the new version the
temp.txt
file looks like this,
line3
line4
The contents written by the first program are completely wiped out.
Isn't the
temp.txt
file supposed to look like this?
line1
line2
line3
line4
If not then what's even the point of using
append
over
write
? Isn't
append
expected to write new content after the previously added content instead of wiping out and starting afresh?
Or, am I missing something?