Writing multiline text files in Lua

Unlike print, the new line character isn't added automatically when calling io.write, you can add it yourself:

file:write("hello", "\n")

The easiest way to achieve this would be to include a Newline character sequence every time you call the write method like so: file:write("hello\n") or so: file:write("hello", "\n"). This way, a script such as

file = io.open(test.txt, "a")
file:write("hello", "\n")
file:write("hello", "\n")

would result in the desired output:

hello
hello

There are, however, many other solutions to this (some being more elegant than the others). When outputting text in Java, for example, there are special methods such as BufferedWriter#newLine(), which will do the same thing in a more cleaner fashion. So if your interested in a different way of achieving this, I suggest you read up on the Lua docs for analogous methods/solutions.