From the course: Android Development Essential Training: Manage Data with Kotlin

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Read files from resources

Read files from resources

From the course: Android Development Essential Training: Manage Data with Kotlin

Start my 1-month free trial

Read files from resources

- [Instructor] Many mobile apps include all the data they need as part of the package that's used to deploy the app. There are a few ways to do this in Android, one of the most efficient is to store files as application resources. The exact format of the data is up to you. It could be an xml or a comma delimited csv file, but most Android apps these days package their data in json format. My app already has this json file, monsterdata.json, and notice that it's stored in a subdirectory of resources called raw. This is where you want to place any text based resource files. I'm going to add some code to read the file's contents into memory. I'll create a new sub-package named utilities and then I'll create a new Kotlin class, and I'll name this FileHelper. I'll set the kind to class, and then when the class is generated, I'll add a companion object. The functions in this class will be designed to be called directly from the class definition, not from an instance of the class. In Java…

Contents