[BAEL-19882] - Move articles out of core-kotlin part1

This commit is contained in:
catalin-burcea
2019-12-10 14:57:11 +02:00
parent 3517462948
commit a141ac01fd
42 changed files with 210 additions and 124 deletions

View File

@@ -0,0 +1,9 @@
## Core Kotlin I/O
This module contains articles about core Kotlin I/O.
### Relevant articles:
- [InputStream to String in Kotlin](https://www.baeldung.com/kotlin-inputstream-to-string)
- [Console I/O in Kotlin](https://www.baeldung.com/kotlin-console-io)
- [Reading from a File in Kotlin](https://www.baeldung.com/kotlin-read-file)
- [Writing to a File in Kotlin](https://www.baeldung.com/kotlin-write-file)

View File

@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-kotlin-io</artifactId>
<name>core-kotlin-io</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-kotlin-modules</groupId>
<artifactId>core-kotlin-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<junit-jupiter.version>5.4.2</junit-jupiter.version>
<mockito.version>2.27.0</mockito.version>
<assertj.version>3.10.0</assertj.version>
</properties>
</project>

View File

@@ -0,0 +1,24 @@
package com.baeldung.filesystem
import java.io.File
class FileReader {
fun readFileLineByLineUsingForEachLine(fileName: String) = File(fileName).forEachLine { println(it) }
fun readFileAsLinesUsingUseLines(fileName: String): List<String> = File(fileName)
.useLines { it.toList() }
fun readFileAsLinesUsingBufferedReader(fileName: String): List<String> = File(fileName).bufferedReader().readLines()
fun readFileAsLinesUsingReadLines(fileName: String): List<String> = File(fileName).readLines()
fun readFileAsTextUsingInputStream(fileName: String) =
File(fileName).inputStream().readBytes().toString(Charsets.UTF_8)
fun readFileDirectlyAsText(fileName: String): String = File(fileName).readText(Charsets.UTF_8)
fun readFileUsingGetResource(fileName: String) = this::class.java.getResource(fileName).readText(Charsets.UTF_8)
fun readFileAsLinesUsingGetResourceAsStream(fileName: String) = this::class.java.getResourceAsStream(fileName).bufferedReader().readLines()
}

View File

@@ -0,0 +1,19 @@
package com.baeldung.filesystem
import java.io.File
class FileWriter {
fun writeFileUsingPrintWriter(fileName: String, fileContent: String) =
File(fileName).printWriter().use { out -> out.print(fileContent) }
fun writeFileUsingBufferedWriter(fileName: String, fileContent: String) =
File(fileName).bufferedWriter().use { out -> out.write(fileContent) }
fun writeFileDirectly(fileName: String, fileContent: String) =
File(fileName).writeText(fileContent)
fun writeFileDirectlyAsBytes(fileName: String, fileContent: String) =
File(fileName).writeBytes(fileContent.toByteArray())
}

View File

@@ -0,0 +1,18 @@
package com.baeldung.inputstream
import java.io.InputStream
fun InputStream.readUpToChar(stopChar: Char): String {
val stringBuilder = StringBuilder()
var currentChar = this.read().toChar()
while (currentChar != stopChar) {
stringBuilder.append(currentChar)
currentChar = this.read().toChar()
if (this.available() <= 0) {
stringBuilder.append(currentChar)
break
}
}
return stringBuilder.toString()
}

View File

@@ -0,0 +1,79 @@
package com.baeldung.console
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock
import java.io.BufferedReader
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.Console
import java.io.InputStreamReader
import java.io.PrintStream
import java.util.*
class ConsoleIOUnitTest {
@Test
fun givenText_whenPrint_thenPrintText() {
val expectedTest = "Hello from Kotlin"
val out = ByteArrayOutputStream()
System.setOut(PrintStream(out))
print(expectedTest)
out.flush()
val printedText = String(out.toByteArray())
assertThat(printedText).isEqualTo(expectedTest)
}
@Test
fun givenInput_whenRead_thenReadText() {
val expectedTest = "Hello from Kotlin"
val input = ByteArrayInputStream(expectedTest.toByteArray())
System.setIn(input)
val readText = readLine()
assertThat(readText).isEqualTo(expectedTest)
}
@Test
fun givenInput_whenReadWithScanner_thenReadText() {
val expectedTest = "Hello from Kotlin"
val scanner = Scanner(ByteArrayInputStream(expectedTest.toByteArray()))
val readText = scanner.nextLine()
assertThat(readText).isEqualTo(expectedTest)
}
@Test
fun givenInput_whenReadWithBufferedReader_thenReadText() {
val expectedTest = "Hello from Kotlin"
val reader = BufferedReader(InputStreamReader(ByteArrayInputStream(expectedTest.toByteArray())))
val readText = reader.readLine()
assertThat(readText).isEqualTo(expectedTest)
}
@Test
fun givenInput_whenReadWithConsole_thenReadText() {
val expectedTest = "Hello from Kotlin"
val console = mock(Console::class.java)
`when`(console.readLine()).thenReturn(expectedTest)
val readText = console.readLine()
assertThat(readText).isEqualTo(expectedTest)
}
@AfterEach
fun resetIO() {
System.setOut(System.out)
System.setIn(System.`in`)
}
}

View File

@@ -0,0 +1,67 @@
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() {
fileReader.readFileLineByLineUsingForEachLine(fileName)
}
@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") }
}
@Test
fun whenReadFileAsTextUsingGetResource_thenCorrect() {
val text = fileReader.readFileUsingGetResource("/Kotlin.in")
assertTrue { text.contains("1. Concise") }
}
@Test
fun whenReadFileUsingGetResourceAsStream_thenCorrect() {
val lines = fileReader.readFileAsLinesUsingGetResourceAsStream("/Kotlin.in")
assertTrue { lines.contains("3. Interoperable") }
}
}

View File

@@ -0,0 +1,43 @@
package com.baeldung.filesystem
import org.junit.jupiter.api.Test
import java.io.File
import kotlin.test.assertEquals
internal class FileWriterTest {
private val fileName = "src/test/resources/Kotlin.out"
private val fileContent = "Kotlin\nConcise, Safe, Interoperable, Tool-friendly"
private val fileWriter = FileWriter()
@Test
fun whenWrittenWithPrintWriter_thenCorrect() {
fileWriter.writeFileUsingPrintWriter(fileName, fileContent)
assertEquals(fileContent, File(fileName).readText())
}
@Test
fun whenWrittenWithBufferedWriter_thenCorrect() {
fileWriter.writeFileUsingBufferedWriter(fileName, fileContent)
assertEquals(fileContent, File(fileName).readText())
}
@Test
fun whenWrittenDirectly_thenCorrect() {
fileWriter.writeFileDirectly(fileName, fileContent)
assertEquals(fileContent, File(fileName).readText())
}
@Test
fun whenWrittenDirectlyAsBytes_thenCorrect() {
fileWriter.writeFileDirectlyAsBytes(fileName, fileContent)
assertEquals(fileContent, File(fileName).readText())
}
}

View File

@@ -0,0 +1,74 @@
package com.baeldung.inputstream
import kotlinx.io.core.use
import org.junit.Test
import java.io.BufferedReader
import java.io.File
import kotlin.test.assertEquals
class InputStreamToStringTest {
private val fileName = "src/test/resources/inputstream2string.txt"
private val endOfLine = System.lineSeparator()
private val fileFullContent = "Computer programming can be a hassle$endOfLine" +
"It's like trying to take a defended castle"
@Test
fun whenReadFileWithBufferedReader_thenFullFileContentIsReadAsString() {
val file = File(fileName)
val inputStream = file.inputStream()
val content = inputStream.bufferedReader().use(BufferedReader::readText)
assertEquals(fileFullContent, content)
}
@Test
fun whenReadFileWithBufferedReaderReadText_thenFullFileContentIsReadAsString() {
val file = File(fileName)
val inputStream = file.inputStream()
val reader = BufferedReader(inputStream.reader())
var content: String
try {
content = reader.readText()
} finally {
reader.close()
}
assertEquals(fileFullContent, content)
}
@Test
fun whenReadFileWithBufferedReaderManually_thenFullFileContentIsReadAsString() {
val file = File(fileName)
val inputStream = file.inputStream()
val reader = BufferedReader(inputStream.reader())
val content = StringBuilder()
try {
var line = reader.readLine()
while (line != null) {
content.append(line)
line = reader.readLine()
}
} finally {
reader.close()
}
assertEquals(fileFullContent.replace(endOfLine, ""), content.toString())
}
@Test
fun whenReadFileUpToStopChar_thenPartBeforeStopCharIsReadAsString() {
val file = File(fileName)
val inputStream = file.inputStream()
val content = inputStream.use { it.readUpToChar(' ') }
assertEquals("Computer", content)
}
@Test
fun whenReadFileWithoutContainingStopChar_thenFullFileContentIsReadAsString() {
val file = File(fileName)
val inputStream = file.inputStream()
val content = inputStream.use { it.readUpToChar('-') }
assertEquals(fileFullContent, content)
}
}

View File

@@ -0,0 +1,5 @@
Hello to Kotlin. Its:
1. Concise
2. Safe
3. Interoperable
4. Tool-friendly

View File

@@ -0,0 +1,2 @@
Kotlin
Concise, Safe, Interoperable, Tool-friendly

View File

@@ -0,0 +1,2 @@
Computer programming can be a hassle
It's like trying to take a defended castle