Kotlin InputStream and OutputStream Tutorial šŸŽÆ

beginner
21 min

Kotlin InputStream and OutputStream Tutorial šŸŽÆ

Welcome to CodeYourCraft's Kotlin InputStream and OutputStream tutorial! In this lesson, we'll delve into the world of streams, learning how to read and write data to various sources like files, network connections, and even memory.

What are InputStream and OutputStream? šŸ“

InputStream and OutputStream are fundamental classes in Kotlin's Java-compatible standard library. They represent streams of bytes and are used for reading and writing data.

  • InputStream is used for reading data from a source like a file, network connection, or even memory.
  • OutputStream is used for writing data to a destination such as a file, network connection, or memory buffer.

Reading Data with InputStream šŸ’”

Let's see how to read data from a file using an InputStream.

kotlin
import java.io.FileInputStream import java.io.File fun main() { val file = File("example.txt") val inputStream = FileInputStream(file) // Reading the file line by line inputStream.bufferedReader().lines().forEach { println(it) } inputStream.close() }

šŸ“ Note: In the above example, we first create a File object for our text file, example.txt. Then, we create an InputStream using the FileInputStream constructor. To read the file line by line, we wrap the InputStream in a BufferedReader and use the lines function to iterate over each line. Lastly, we close the InputStream to release any resources it's holding.

Writing Data with OutputStream šŸ’”

Now, let's write data to a file using an OutputStream.

kotlin
import java.io.FileOutputStream import java.io.File fun main() { val file = File("output.txt") val outputStream = FileOutputStream(file) // Writing text to the file outputStream.write("Hello, World!\n".toByteArray()) outputStream.close() }

šŸ“ Note: Here, we create a FileOutputStream instance for our output file, output.txt. We convert our text string to bytes using the toByteArray() function and write the data to the file using the write() function. Lastly, we close the OutputStream to save the data to the file.

Advanced InputStream and OutputStream Usage šŸ’”

For more advanced usage, you can read and write data in chunks or handle exceptions for better error handling.

kotlin
import java.io.FileInputStream import java.io.FileOutputStream import java.io.IOException fun main() { val inputFile = File("input.bin") val outputFile = File("output.bin") val inputStream: InputStream val outputStream: OutputStream try { inputStream = FileInputStream(inputFile) outputStream = FileOutputStream(outputFile) val buffer = ByteArray(1024) var bytesRead: Int while (true) { bytesRead = inputStream.read(buffer) if (bytesRead <= 0) break outputStream.write(buffer, 0, bytesRead) } inputStream.close() outputStream.close() } catch (e: IOException) { println("An error occurred: ${e.message}") } }

šŸ“ Note: In this example, we read data from input.bin and write it to output.bin in chunks of 1024 bytes. We handle exceptions using the try-catch block to catch any potential errors.

Quick Quiz
Question 1 of 1

What are the main differences between InputStream and OutputStream?