File tree Expand file tree Collapse file tree
main/kotlin/com/baeldung/filesystem
kotlin/com/baeldung/filesystem Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com.baeldung.filesystem
2+
3+ import java.io.BufferedReader
4+ import java.io.File
5+ import java.io.InputStream
6+
7+ class FileReader {
8+
9+ fun readFileLineByLineUsingForEachLine (fileName : String ): List <String > {
10+ val lineList = mutableListOf<String >()
11+ File (fileName).forEachLine { line -> lineList.add(line) }
12+ return lineList
13+ }
14+
15+ fun readFileAsLinesUsingUseLines (fileName : String ): List <String > {
16+ val lineList = mutableListOf<String >()
17+ File (fileName).useLines { lines -> lineList.addAll(lines) }
18+ return lineList
19+ }
20+
21+ fun readFileAsLinesUsingBufferedReader (fileName : String ): List <String > {
22+ val bufferedReader: BufferedReader = File (fileName).bufferedReader()
23+ return bufferedReader.readLines()
24+ }
25+
26+ fun readFileAsLinesUsingReadLines (fileName : String ): List <String > {
27+ return File (fileName).readLines()
28+ }
29+
30+ fun readFileAsTextUsingInputStream (fileName : String ): String {
31+ val inputStream: InputStream = File (fileName).inputStream()
32+ return inputStream.readBytes().toString(Charsets .UTF_8 )
33+ }
34+
35+ fun readFileDirectlyAsText (fileName : String ): String {
36+ return File (fileName).readText(Charsets .UTF_8 )
37+ }
38+
39+ }
Original file line number Diff line number Diff line change 1+ package com.baeldung.filesystem
2+
3+ import org.junit.jupiter.api.Test
4+ import kotlin.test.assertTrue
5+
6+ internal class FileReaderTest {
7+
8+ private val fileName = " src/test/resources/Kotlin.in"
9+
10+ private val fileReader = FileReader ()
11+
12+ @Test
13+ fun whenReadFileLineByLineUsingForEachLine_thenCorrect () {
14+ val lines = fileReader.readFileLineByLineUsingForEachLine(fileName)
15+
16+ assertTrue { lines.contains(" Hello to Kotlin. Its:" ) }
17+ }
18+
19+ @Test
20+ fun whenReadFileAsLinesUsingUseLines_thenCorrect () {
21+ val lines = fileReader.readFileAsLinesUsingUseLines(fileName)
22+
23+ assertTrue { lines.contains(" 1. Concise" ) }
24+ }
25+
26+ @Test
27+ fun whenReadFileAsLinesUsingBufferedReader_thenCorrect () {
28+ val lines = fileReader.readFileAsLinesUsingBufferedReader(fileName)
29+
30+ assertTrue { lines.contains(" 2. Safe" ) }
31+ }
32+
33+ @Test
34+ fun whenReadFileAsLinesUsingReadLines_thenCorrect () {
35+ val lines = fileReader.readFileAsLinesUsingReadLines(fileName)
36+
37+ assertTrue { lines.contains(" 3. Interoperable" ) }
38+ }
39+
40+ @Test
41+ fun whenReadFileAsTextUsingInputStream_thenCorrect () {
42+ val text = fileReader.readFileAsTextUsingInputStream(fileName)
43+
44+ assertTrue { text.contains(" 4. Tool-friendly" ) }
45+ }
46+
47+ @Test
48+ fun whenReadDirectlyAsText_thenCorrect () {
49+ val text = fileReader.readFileDirectlyAsText(fileName)
50+
51+ assertTrue { text.contains(" Hello to Kotlin" ) }
52+ }
53+ }
Original file line number Diff line number Diff line change 1+ Hello to Kotlin. Its:
2+ 1. Concise
3+ 2. Safe
4+ 3. Interoperable
5+ 4. Tool-friendly
You can’t perform that action at this time.
0 commit comments