Java: Memory efficient ByteArrayOutputStream

前端 未结 9 582
一个人的身影
一个人的身影 2021-02-04 06:15

I\'ve got a 40MB file in the disk and I need to \"map\" it into memory using a byte array.

At first, I thought writing the file to a ByteArrayOutputStream would be the b

9条回答
  •  你的背包
    2021-02-04 06:26

    If you really want to map the file into memory, then a FileChannel is the appropriate mechanism.

    If all you want to do is read the file into a simple byte[] (and don't need changes to that array to be reflected back to the file), then simply reading into an appropriately-sized byte[] from a normal FileInputStream should suffice.

    Guava has Files.toByteArray() which does all that for you.

提交回复
热议问题