Created
February 20, 2022 07:19
-
-
Save devrath/d899dff7ece94dd2cfc15347b4e17b51 to your computer and use it in GitHub Desktop.
File handling using the kotlin functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class FileHelper { | |
| companion object { | |
| // From RAW folder | |
| fun retrieveTextFromResources(context: Context, resourceId: Int): String { | |
| return context.resources.openRawResource(resourceId).use { inputStream -> | |
| // Here we get reference to input stream | |
| inputStream.bufferedReader().use { bufferedReader -> | |
| // Here we get reference to buffered reader | |
| bufferedReader.readText() | |
| } | |
| } | |
| } | |
| // From ASSETS folder | |
| fun retrieveTextFromAssets(context: Context, fileName: String): String { | |
| return context.assets.open(fileName).use { inputStream -> | |
| // Here we get reference to input stream | |
| inputStream.bufferedReader().use { bufferedReader -> | |
| // Here we get reference to buffered reader | |
| bufferedReader.readText() | |
| } | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Say you are retrieving the data from a assets .txt file kept in assets folder, We access as follows | |
| val text = FileHelper.retrieveTextFromResources(app, "our_sample_data.json") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment