Skip to content

Instantly share code, notes, and snippets.

@rodrigohenriques
Created July 29, 2016 20:19
Show Gist options
  • Save rodrigohenriques/146cc0d612c9c67717803152b97cf1bf to your computer and use it in GitHub Desktop.
Save rodrigohenriques/146cc0d612c9c67717803152b97cf1bf to your computer and use it in GitHub Desktop.
Read file content using recursion with Kotlin
fun readFileContent(file: File) : String {
val reader = FileReader(file)
fun readBuffer(bufferedReader: BufferedReader) : String {
val result = StringBuilder()
fun finally() : String {
bufferedReader.close()
return result.toString()
}
tailrec fun readLine() : String {
val line = bufferedReader.readLine() ?: return finally()
result.append(line).append("\n")
return readLine()
}
return readLine()
}
return readBuffer(BufferedReader(reader))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment