title: How do I write to a file in Kotlin?
body: <p>I can't seem to find this question yet, but what is the simplest, most-idiomatic way of opening/creating a file, writing to it, and then closing it? Looking at the <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/index.html" rel="noreferrer">kotlin.io</a> reference and the Java documentation I managed to get this:</p>
  
  <pre><code>fun write() {
      val writer = PrintWriter("file.txt")  // java.io.PrintWriter
  
      for ((member, originalInput) in history) {  // history: Map&lt;Member, String&gt;
          writer.append("$member, $originalInput\n")
      }
  
      writer.close()
  }
  </code></pre>
  
  <p>This works, but I was wondering if there was a "proper" Kotlin way of doing this?</p>