Skip to content

Commit 139e281

Browse files
committed
Add readtext example
1 parent 990610e commit 139e281

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

‎core-kotlin-2/src/test/kotlin/com/baeldung/inputstream/InputStreamToStringTest.kt‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ class InputStreamToStringTest {
1010
private val fileName = "src/test/resources/inputstream2string.txt"
1111
private val fileFullContent = "Computer programming can be a hassle\r\n" +
1212
"It's like trying to take a defended castle"
13-
private val fileContentUpToStopChar = "Computer programming can be a hassle\r\n" +
14-
"It"
1513

1614
@Test
1715
fun whenReadFileWithBufferedReader_thenFullFileContentIsReadAsString() {
@@ -20,7 +18,19 @@ class InputStreamToStringTest {
2018
val content = inputStream.bufferedReader().use(BufferedReader::readText)
2119
assertEquals(fileFullContent, content)
2220
}
23-
21+
@Test
22+
fun whenReadFileWithBufferedReaderReadText_thenFullFileContentIsReadAsString() {
23+
val file = File(fileName)
24+
val inputStream = file.inputStream()
25+
val reader = BufferedReader(inputStream.reader())
26+
var content: String
27+
try {
28+
content = reader.readText()
29+
} finally {
30+
reader.close()
31+
}
32+
assertEquals(fileFullContent, content)
33+
}
2434
@Test
2535
fun whenReadFileWithBufferedReaderManually_thenFullFileContentIsReadAsString() {
2636
val file = File(fileName)
@@ -44,8 +54,8 @@ class InputStreamToStringTest {
4454
fun whenReadFileUpToStopChar_thenPartBeforeStopCharIsReadAsString() {
4555
val file = File(fileName)
4656
val inputStream = file.inputStream()
47-
val content = inputStream.use { it.readUpToChar('\'') }
48-
assertEquals(fileContentUpToStopChar, content)
57+
val content = inputStream.use { it.readUpToChar(' ') }
58+
assertEquals("Computer", content)
4959
}
5060

5161
@Test

0 commit comments

Comments
 (0)