Reading a txt file and outputing as a TextView in Android

后端 未结 4 1728
心在旅途
心在旅途 2021-01-07 09:46

I am trying to read a text file that is already saved in my directory and print it on the screen as a TextView. This is the code that I have so far. However, when I run the

4条回答
  •  滥情空心
    2021-01-07 10:08

    This is the Kotlin version of the answer from above:

    var text = ""
    var reader: BufferedReader? = null
    
    try {
        reader = BufferedReader(InputStreamReader(assets.open("inputNews.txt")))
        text = reader.readLines().joinToString("\n")
    } catch (e: IOException) {
        Toast.makeText(applicationContext, "Error reading license file!", Toast.LENGTH_SHORT).show()
        e.printStackTrace()
    } finally {
        try {
            reader?.close()
        } catch (e: IOException) {
            //log the exception
            e.printStackTrace()
        }
        textView.text = text
    }
    

提交回复
热议问题