joshua-r2
11/27/2017 - 8:54 PM

Saving and Opening Files using Internal Directory

This is the easiest sample of how to read and write simple txt files in Kotlin for Android.

class SomeActivity: AppCompatActivity(){

  fun saveAThing(thing: String){
        File(this.filesDir, "things.txt").bufferedWriter().use { out ->
            out.write(thing)
        }
        
  fun openThings(){
        var printText = ""

        File(this.filesDir, "things.txt").bufferedReader().forEachLine { line ->
            printText += line
        }
        
        println(printText)
    }

}