最简单的方法是使用
File("aaa").readBytes()
那个人会把整个文件读到ByteArray
.但是您应该小心地知道堆中有足够的RAM来执行此操作
The ByteArray can be created via ByteArray(100)
call, where 100
is the size of it
For the RandomAccessFile
, it is probably better to use at the readFully
function, which reads exactly the requested amount of bytes.
The classic approach is possible to read a file by chunks, e.g.
val buff = ByteArray(1230)
File("aaa").inputStream().buffered().use { input ->
while(true) {
val sz = input.read(buff)
if (sz <= 0) break
///at that point we have a sz bytes in the buff to process
consumeArray(buff, 0, sz)
}
}