Kotlin Program to Convert File to Byte Array and Vice-Versa

Converting a file to a byte array and vice versa is a common task in Kotlin when dealing with file handling and IO operations. It involves reading the contents of a file into a byte array or writing a byte array to a file. In this article, we will explore three different Kotlin Program to Convert File to Byte Array and Vice-Versa

1. Using FileInputStream and FileOutputStream

1.1 Example Code to Convert File to Byte Array:

Kotlin
import java.io.*

fun main() {
    val file = File("sample.txt")
    val fileBytes = file.readBytes()

    println("File to Byte Array:")
    println(fileBytes.joinToString())

    // Convert Byte Array back to File
    val newFile = File("newSample.txt")
    newFile.writeBytes(fileBytes)
    println("\nByte Array to File: newSample.txt")
}

Explanation:

In this example, we use FileInputStream to read the contents of a file into a byte array using readBytes(). We then print the byte array converted from the file. To convert the byte array back to a file, we use FileOutputStream and write the byte array to a new file.

Output:

Kotlin
File to Byte Array:
72, 101, 108, 108, 111, 32, 75, 111, 116, 108, 105, 110!

Byte Array to File: newSample.txt

2. Using Files API in Java

2.1 Example Code to Convert File to Byte Array:

Kotlin
import java.nio.file.*

fun main() {
    val filePath = Paths.get("sample.txt")
    val fileBytes = Files.readAllBytes(filePath)

    println("File to Byte Array:")
    println(fileBytes.joinToString())

    // Convert Byte Array back to File
    val newFilePath = Paths.get("newSample.txt")
    Files.write(newFilePath, fileBytes)
    println("\nByte Array to File: newSample.txt")
}

Explanation:

In this example, we use the Files API from Java’s NIO package to read the contents of a file into a byte array using readAllBytes(). We then print the byte array converted from the file. To convert the byte array back to a file, we use Files.write() to write the byte array to a new file.

Output:

Kotlin
File to Byte Array:
72, 101, 108, 108, 111, 32, 75, 111, 116, 108, 105, 110!

Byte Array to File: newSample.txt

3. Using BufferedInputStream and BufferedOutputStream

3.1 Example Code to Convert File to Byte Array:

Kotlin
import java.io.*

fun main() {
    val file = File("sample.txt")
    val fileBytes = BufferedInputStream(FileInputStream(file)).use { it.readBytes() }

    println("File to Byte Array:")
    println(fileBytes.joinToString())

    // Convert Byte Array back to File
    val newFile = File("newSample.txt")
    BufferedOutputStream(FileOutputStream(newFile)).use { it.write(fileBytes) }
    println("\nByte Array to File: newSample.txt")
}

In this example, we use BufferedInputStream to read the contents of a file into a byte array using readBytes(). We then print the byte array converted from the file. To convert the byte array back to a file, we use BufferedOutputStream to write the byte array to a new file.

Output:

Kotlin
File to Byte Array:
72, 101, 108, 108, 111, 32, 75, 111, 116, 108, 105, 110!

Byte Array to File: newSample.txt

Summary and Conclusion

Converting a file to a byte array and vice versa in Kotlin can be achieved using several methods:

  1. Using FileInputStream and FileOutputStream for straightforward file IO operations.
  2. Utilizing the Files API from Java’s NIO package for more streamlined file handling.
  3. Using BufferedInputStream and BufferedOutputStream for efficient buffering and IO handling.

Each approach has its own advantages and is suitable for different scenarios. Understanding these methods empowers developers to efficiently handle file to byte array conversions in their Kotlin programs, enhancing the reliability and flexibility of their file handling code.