I would like to read the content of a file located in the Assets as a String. For example, a text document located in src/main/assets/
Original
getAssets().open() will return an InputStream. Read from that using standard Java I/O:
Java:
StringBuilder sb = new StringBuilder();
InputStream is = getAssets().open("book/contents.json");
BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8 ));
String str;
while ((str = br.readLine()) != null) {
sb.append(str);
}
br.close();
Kotlin:
val str = assets.open("book/contents.json").bufferedReader().use { it.readText() }