Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a Text asset(text file from assets folder) as a String in Kotlin (Android)

I need to read a text file stored at src/main/assets/ i.e; in the assets folder and get it as a string.

Is there a simple way to do it.

Java copy, paste, convert functions are giving trouble, so I'd rather use a kotlin way.

I need a kotlin way to do this

like image 911
Kotlinboy Avatar asked Nov 09 '17 12:11

Kotlinboy


People also ask

How do you read an asset file?

Right click on the assets folder, select New >> file (myText. txt) and your text.

How does kotlin read data from a text file?

1) Select "Project" from the top of the vertical toolbar to open the project "tool window" 2) From the drop-down menu at the top of the "tool window", select "Android" 3) Right-click on "App" and select "New" then -> "Folder" (the one with the green Android icon beside it) then -> "Assets Folder" 4) Right-click on the ...

How do I get an asset file on Android?

Step 1: To create an asset folder in Android studio open your project in Android mode first as shown in the below image. Step 2: Go to the app > right-click > New > Folder > Asset Folder and create the asset folder. Step 3: Android Studio will open a dialog box. Keep all the settings default.


1 Answers

I found this in a youtube video. Here is the link https://www.youtube.com/watch?v=o5pDghyRHmI

val file_name = "qjsonfile.json" val json_string = application.assets.open(file_name).bufferedReader().use{             it.readText()         } 

Saves the JSON or text to the string json_string.

like image 130
Kotlinboy Avatar answered Oct 01 '22 10:10

Kotlinboy