Kotlin File转ByteArray

方法1:

FileInputStream(this).use { input ->
    var offset = 0
    var remaining = this.length().toInt()
    val result = ByteArray(remaining)
    while (remaining > 0) {
        val read = input.read(result, offset, remaining)
        if (read < 0) break
        remaining -= read
        offset += read
    }
    if (remaining == 0) result else result.copyOf(offset)

方法2:

File(path).readBytes()

你可能感兴趣的:(Kotlin)