From b3064e448e46032415549621954a3603b31b5044 Mon Sep 17 00:00:00 2001 From: Siben Nayak Date: Sun, 1 Apr 2018 00:47:42 +0530 Subject: [PATCH 1/2] Kotlin Read from File --- .../com/baeldung/filesystem/FileReader.kt | 40 ++++++++++++++ .../com/baeldung/filesystem/FileReaderTest.kt | 53 +++++++++++++++++++ core-kotlin/src/test/resources/Kotlin.in | 5 ++ 3 files changed, 98 insertions(+) create mode 100644 core-kotlin/src/main/kotlin/com/baeldung/filesystem/FileReader.kt create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/filesystem/FileReaderTest.kt create mode 100644 core-kotlin/src/test/resources/Kotlin.in diff --git a/core-kotlin/src/main/kotlin/com/baeldung/filesystem/FileReader.kt b/core-kotlin/src/main/kotlin/com/baeldung/filesystem/FileReader.kt new file mode 100644 index 0000000000..6897a87cb2 --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/filesystem/FileReader.kt @@ -0,0 +1,40 @@ +package com.baeldung.filesystem + +import java.io.BufferedReader +import java.io.File +import java.io.InputStream +import java.nio.charset.StandardCharsets + +class FileReader { + + fun readFileLineByLineUsingForEachLine(fileName: String): List { + val lineList = mutableListOf() + File(fileName).forEachLine { line -> lineList.add(line) } + return lineList + } + + fun readFileAsLinesUsingUseLines(fileName: String): List { + val lineList = mutableListOf() + File(fileName).useLines { lines -> lineList.addAll(lines) } + return lineList + } + + fun readFileAsLinesUsingBufferedReader(fileName: String): List { + val bufferedReader: BufferedReader = File(fileName).bufferedReader() + return bufferedReader.readLines() + } + + fun readFileAsLinesUsingReadLines(fileName: String): List { + return File(fileName).readLines(StandardCharsets.UTF_8) + } + + fun readFileAsTextUsingInputStream(fileName: String): String { + val inputStream: InputStream = File(fileName).inputStream() + return inputStream.readBytes().toString(StandardCharsets.UTF_8) + } + + fun readFileDirectlyAsText(fileName: String): String { + return File(fileName).readText(StandardCharsets.UTF_8) + } + +} \ No newline at end of file diff --git a/core-kotlin/src/test/kotlin/com/baeldung/filesystem/FileReaderTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/filesystem/FileReaderTest.kt new file mode 100644 index 0000000000..fd628b8448 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/filesystem/FileReaderTest.kt @@ -0,0 +1,53 @@ +package com.baeldung.filesystem + +import org.junit.jupiter.api.Test +import kotlin.test.assertTrue + +internal class FileReaderTest { + + private val fileName = "src/test/resources/Kotlin.in" + + private val fileReader = FileReader() + + @Test + fun whenReadFileLineByLineUsingForEachLine_thenCorrect() { + val lines = fileReader.readFileLineByLineUsingForEachLine(fileName) + + assertTrue { lines.contains("Hello to Kotlin. Its:") } + } + + @Test + fun whenReadFileAsLinesUsingUseLines_thenCorrect() { + val lines = fileReader.readFileAsLinesUsingUseLines(fileName) + + assertTrue { lines.contains("1. Concise") } + } + + @Test + fun whenReadFileAsLinesUsingBufferedReader_thenCorrect() { + val lines = fileReader.readFileAsLinesUsingBufferedReader(fileName) + + assertTrue { lines.contains("2. Safe") } + } + + @Test + fun whenReadFileAsLinesUsingReadLines_thenCorrect() { + val lines = fileReader.readFileAsLinesUsingReadLines(fileName) + + assertTrue { lines.contains("3. Interoperable") } + } + + @Test + fun whenReadFileAsTextUsingInputStream_thenCorrect() { + val text = fileReader.readFileAsTextUsingInputStream(fileName) + + assertTrue { text.contains("4. Tool-friendly") } + } + + @Test + fun whenReadDirectlyAsText_thenCorrect() { + val text = fileReader.readFileDirectlyAsText(fileName) + + assertTrue { text.contains("Hello to Kotlin") } + } +} \ No newline at end of file diff --git a/core-kotlin/src/test/resources/Kotlin.in b/core-kotlin/src/test/resources/Kotlin.in new file mode 100644 index 0000000000..d140d4429e --- /dev/null +++ b/core-kotlin/src/test/resources/Kotlin.in @@ -0,0 +1,5 @@ +Hello to Kotlin. Its: +1. Concise +2. Safe +3. Interoperable +4. Tool-friendly \ No newline at end of file From 6fadb8bdefec048898d151cd12fbf99e45457d28 Mon Sep 17 00:00:00 2001 From: Siben Nayak Date: Mon, 2 Apr 2018 16:34:35 +0530 Subject: [PATCH 2/2] Change StandardCharsets to Charsets --- .../src/main/kotlin/com/baeldung/filesystem/FileReader.kt | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) 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 6897a87cb2..05a2ac03bb 100644 --- a/core-kotlin/src/main/kotlin/com/baeldung/filesystem/FileReader.kt +++ b/core-kotlin/src/main/kotlin/com/baeldung/filesystem/FileReader.kt @@ -3,7 +3,6 @@ package com.baeldung.filesystem import java.io.BufferedReader import java.io.File import java.io.InputStream -import java.nio.charset.StandardCharsets class FileReader { @@ -25,16 +24,16 @@ class FileReader { } fun readFileAsLinesUsingReadLines(fileName: String): List { - return File(fileName).readLines(StandardCharsets.UTF_8) + return File(fileName).readLines() } fun readFileAsTextUsingInputStream(fileName: String): String { val inputStream: InputStream = File(fileName).inputStream() - return inputStream.readBytes().toString(StandardCharsets.UTF_8) + return inputStream.readBytes().toString(Charsets.UTF_8) } fun readFileDirectlyAsText(fileName: String): String { - return File(fileName).readText(StandardCharsets.UTF_8) + return File(fileName).readText(Charsets.UTF_8) } } \ No newline at end of file