The readBytes() reads the entire content of a file as a byte array. It is not recommended on huge files.
Kotlin read a file with readBytes
The example reads a text file into a byte array. It prints the file as numbers to the console.
import java.io.File
fun main(args: Array<String>) {
val fileName = "src/resources/thermopylae.txt"
val file = File(fileName)
var bytes: ByteArray = file.readBytes()
bytes.forEachIndexed { i, byte -> (
if (i == 0) {
print("${byte} ")
} else if (i % 10 == 0) {
print("${byte} \n")
} else {
print("${byte} ")
})
}
}
Comments
Post a Comment