Home > database >  File reading in Kotlin without piggybacking off a button
File reading in Kotlin without piggybacking off a button

Time:08-25

I am coding a data class that is wanting to read a csv file to grab some information that is stored on the file. How ever, every way that I have tried to read the file will not work.

Here is what I have tried so far:

data class Bird(val birdNumIn: Int){
private var birdNum = birdNumIn


/**
 * A function that searches the bird-data.csv file which is a list of birds to find the bird that was
 * inputted into the class constructor and then add the values of the bird to the private variables.
 */
fun updateValues(){



    var birdNumber = birdNum
    var birdInfo: MutableList<String> = mutableListOf()

    val minput = InputStreamReader(assets().open("bird-data.csv"), "UTF-8")
    val reader = BufferedReader(minput)
}

How ever the assets().open() does not work. It returns an error of trying to open a file that does not exist, but the is in the assets folder, and the filename is spelt right.

I have tried many other methods on trying to read files, like using Java.io.File and using the path of the file.

If you would like to look at our whole project, please feel free to go to our github

CodePudding user response:

What's the assets() function you're calling? This is just a bare data class, it has no connection to the Android environment it's running in, so unless you've injected an AssetManager instance (or a Context to pull it from) into your class, you can't access it.

You probably need to do this:

fun updateValues(context: Context){
    val inputStream = context.assets.open("bird-data.csv")
    val minput = InputStreamReader(inputStream, "UTF-8")
    ...
}

which requires your caller to have access to a Context.


Honestly from a quick look at your class, you might want to rework this. Instead of having a bunch of empty fields in your data class (which aren't part of the "data" by the way, only stuff in the constructor parameters is), and then having those updated later by the data class doing some IO, you might want to keep them as just basic stores of data, and create them when you read from your assets file.

So something like:

// all fixed values, initialised during construction
// Also you won't need to override toString now (unless you want to)
data class Bird(
    val birdNum: Int
    val nameOfBird: String
    val birdFilePic: String
    val birdFileSong: String
    val alternativeName: String
    val birdInfoFile: String
) { ... }

Then somewhere else

fun getBirbs(context: Context) {
    // open CSV
    // read the lines
    val allBirds = lines.map {
        // parse data for each bird, use it to construct a Bird object
    }
}

or whatever you need to do, e.g. loading certain birds by ID.

That way your Bird class is just data and some functions/properties that work with it, it doesn't need a Context because it's not doing any I/O. Something else (that does have access to a Context) is responsible for loading your data and turning it into objects - deserialising it basically. And as soon as it's created, it's ready and initialised and immutable - you don't have to call update on it to get it actually initialised.

And if you ever wanted to do that a different way (e.g. loading a file from the internet) the data class wouldn't need to change, just the thing that does the loading. You could even have different loading classes! One that loads local data, one that fetches from the internet. The point is the separation of concerns, so it's possible to do this kind of thing because that functionality isn't baked into a class that's really about something else.

Up to you but just a thought! Especially if passing the context in like I suggested is a problem - that's a sign your design might need tweaking

  • Related