Created
July 29, 2016 20:19
-
-
Save rodrigohenriques/146cc0d612c9c67717803152b97cf1bf to your computer and use it in GitHub Desktop.
Read file content using recursion with Kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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