How to read and write txt files in android in kotlin

后端 未结 5 1127
小鲜肉
小鲜肉 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:55

    fun loadWords(context: Context): ArrayList {
            
        val Word = ArrayList()
            var line: String
            var word = ""
            var weight = 0
            try {
                val reader: BufferedReader
                val file = context.assets.open("spam_keywords.txt")
                reader = BufferedReader(InputStreamReader(file))
                while ((reader.readLine()) != null) {
                    line = reader.readLine()
                    val st = StringTokenizer(line)
                    while (st.hasMoreElements()) {
                        word = st.nextElement().toString()
                    }
                    Word.add(word)
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
            println(Word)
            return Word 
        }
    

提交回复
热议问题