I have already done with java but find difficult with Kotlin.
I have already search with google but none of them work for me.
/**
* Get the json data from json file.
*
* @param context the context to acces the resources.
* @param fileName the name of the json file
* @return json as string
*/
public static String getJsonFromAsset(Context context, String fileName) {
String json = "";
try {
InputStream stream = context.getAssets().open(fileName);
int size = stream.available();
byte[] buffer = new byte[size];
stream.read(buffer);
stream.close();
json = new String(buffer, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
I want this code in Kotlin.
Reading json file from assets folder in Kotlin is very easy, just use the following code
val fileInString: String =
applicationContext.assets.open(fileName).bufferedReader().use { it.readText() }