title: How to create a text file and write to it in Kotlin?
body: <p>I am new to Kotlin/Android development. I am trying to simply write text to a text file. I simply cannot figure out how to do this. I have tried doing:</p>
  
  <pre><code>File("filename.txt").printWriter().use { out -&gt;
      out.println("${it.key}, ${it.value}")
  }
  </code></pre>
  
  <p>And I got the following. (Side note, "filename.txt" is located in the assets folder in my project)</p>
  
  <pre><code>Caused by: java.io.FileNotFoundException: prices.txt (Read-only file system)
  </code></pre>
  
  <p>So I figured out that I need to be writing to a spot where I have permission. To my knowledge this is internal private storage. I figured out which directory I have permissions to write to by using:</p>
  
  <pre><code>filesDir
  </code></pre>
  
  <p>That gave me:</p>
  
  <pre><code>/data/user/com.example.myname.appname/files
  </code></pre>
  
  <p>So from what I have seen so far I just need to create a file in this directory, write to said file, and read from it when I would like. The problem is I don't know how to do that. But I tried doing this:</p>
  
  <pre><code>// create file?
  val file = File(applicationContext.filesDir, "test.txt")
  //try to write to said file?
  applicationContext.openFileOutput(file.toString(), Context.MODE_PRIVATE).use 
  {
      it.write("test".toByteArray())
  }
  </code></pre>
  
  <p>But then I get this error:</p>
  
  <pre><code>Caused by: java.lang.IllegalArgumentException: File    
  /data/user/0/com.example.pawlaczykm.dollarsense/files/test.txt contains        
  a path separator
  </code></pre>
  
  <p>I am at the point of maximum misunderstanding and confusion. Again, my goal is to write something to a text file, then access the file later throughout the application.</p>
  
  <h2>Update</h2>
  
  <p>Tried the following and didn't see back "comment":</p>
  
  <pre><code>File(applicationContext.filesDir, "test.txt").printWriter().use{ out -&gt;
          out.println("content")
      }
  File(applicationContext.filesDir, "test.txt").bufferedReader().use{ out -&gt;
          var text5 = out.toString()
          Toast.makeText(this, text5.toString(), Toast.LENGTH_LONG).show()
      }
  </code></pre>