How to read and write txt files in android in kotlin

后端 未结 5 1055
小鲜肉
小鲜肉 2021-02-05 22:17

I\'m still pretty much a beginner in kotlin and android studio. I can access most of the android widgets but I cannot access files and so far I managed to come across only the f

5条回答
  •  感情败类
    2021-02-05 22:56

    In 5 lines: create file to internal directory if not exists, write to file, read file

    val file = File(ctx.filesDir, FILE_NAME)
    file.createNewFile()
    file.appendText("record goes here")
    val readResult = FileInputStream(file).bufferedReader().use { it.readText() }
    println("readResult=$readResult")
    

提交回复
热议问题