# write into a buffer and then use writer.writeAll to push that all to stdout at the same time (or use a buffered writer and flush at the end of each iteration)