How To Read Entire Text / JSON File in 1 Line of Kotlin Code
I wanted to read an entire json file which contains my test request. I found this very quick and super simple 1 line to read an entire file as string.
Quickest Solution
File("path/my-test.json").readText()
Some other alternatives to read file
Read all lines as ArrayList<String>
val lines = Files.readAllLines( encoding) println(lines)
Reading as bytes
val encoded = Files.readAllBytes(Paths.get("my-test.json")) val asText = String(encoded, encoding)
Comments
Leave a comment
You are not LoggedIn but you can comment as an anonymous user which requires manual approval. For better experience please Login .