1

I am trying to develop my first app on android using Kotlin. For the moment I try to read a file located in my android project. I have the following architecture under my "Java" folder :

/java/example.first.com.monapp/controller/FirstActivity.kt
/java/example.first.com.monapp/controller/WelcomeActivity.kt
/java/example.first.com.monapp/model/words.kt
/java/example.first.com.monapp/model/wordsData

wordsData is a text file with raw data (3 words per line separated by a ";")

The code in words.kt is :

package example.first.com.monapp.model

import java.io.File

data class Word(val wordFr:String, val wordRu:String, val active:Boolean)

fun readFileKotlin(): List<Word> {

    val fileToRead="wordsData"
    val wordList = mutableListOf<Word>()
    var reader= File(fileToRead).readLines()
    var wrdLst:List<Word>

    for (line in reader) {
        val wrdProperties = line.split(";")
        wordList.add(Word(wrdProperties[0], wrdProperties[1], wrdProperties[2].toBoolean()))
    }
    wrdLst=wordList
    return wrdLst
}

I call the readFileKotlin function during the creation of my activity but I continuously get the "java.io.FileNotFoundException" No such file or directory error.

I tried several variant to the path such as

val fileToRead="model/wordsData"

or

val fileToRead="/java/example.first.com.monapp/model/"

or

val fileToRead="/java/example/first/com/monapp/model/"

but none of this works.

What am I doing wrong ?

Thanks in advance !

Flx
  • 13
  • 1
  • 4

1 Answers1

1

put the wordsData file into the android assets folder, it is the folder where you can put raw data assets in android

Then in your Activity you can get an InputStream like this:

getAssets().open("wordsData.txt")
donfuxx
  • 11,277
  • 6
  • 44
  • 76