title: How do I write to a file in Kotlin? Nothing I've found works
body: <p>I'm having trouble with writing to a file. Here is my code:</p>
  
  <pre><code>File("test.txt").printWriter().use { out -&gt;
              out.println("First line")
              out.println("Second line")
  </code></pre>
  
  <p>I've looked that the answers at <a href="https://stackoverflow.com/questions/35444264/how-do-i-write-to-a-file-in-kotlin">How do I write to a file in Kotlin?</a> but they don't work; gradle builds and runs but <code>test.txt</code> stays empty. I've also checked the Kotlin documentation but I can't find anything. </p>
  
  <p>Thanks!</p>
  
  <p>Edit: If I put the full path of the file then it works.</p>