diff --git a/core-kotlin/src/main/kotlin/com/baeldung/filesystem/FileReader.kt b/core-kotlin/src/main/kotlin/com/baeldung/filesystem/FileReader.kt index 05a2ac03bb..8539378c91 100644 --- a/core-kotlin/src/main/kotlin/com/baeldung/filesystem/FileReader.kt +++ b/core-kotlin/src/main/kotlin/com/baeldung/filesystem/FileReader.kt @@ -1,39 +1,20 @@ package com.baeldung.filesystem -import java.io.BufferedReader import java.io.File -import java.io.InputStream class FileReader { - fun readFileLineByLineUsingForEachLine(fileName: String): List { - val lineList = mutableListOf() - File(fileName).forEachLine { line -> lineList.add(line) } - return lineList - } + fun readFileLineByLineUsingForEachLine(fileName: String) = File(fileName).forEachLine { println(it) } - fun readFileAsLinesUsingUseLines(fileName: String): List { - val lineList = mutableListOf() - File(fileName).useLines { lines -> lineList.addAll(lines) } - return lineList - } + fun readFileAsLinesUsingUseLines(fileName: String): List = File(fileName) + .useLines { it.toList() } - fun readFileAsLinesUsingBufferedReader(fileName: String): List { - val bufferedReader: BufferedReader = File(fileName).bufferedReader() - return bufferedReader.readLines() - } + fun readFileAsLinesUsingBufferedReader(fileName: String): List = File(fileName).bufferedReader().readLines() - fun readFileAsLinesUsingReadLines(fileName: String): List { - return File(fileName).readLines() - } + fun readFileAsLinesUsingReadLines(fileName: String): List = File(fileName).readLines() - fun readFileAsTextUsingInputStream(fileName: String): String { - val inputStream: InputStream = File(fileName).inputStream() - return inputStream.readBytes().toString(Charsets.UTF_8) - } - - fun readFileDirectlyAsText(fileName: String): String { - return File(fileName).readText(Charsets.UTF_8) - } + fun readFileAsTextUsingInputStream(fileName: String) = + File(fileName).inputStream().readBytes().toString(Charsets.UTF_8) + fun readFileDirectlyAsText(fileName: String): String = File(fileName).readText(Charsets.UTF_8) } \ No newline at end of file