Java: Efficiently converting an array of longs to an array of bytes

后端 未结 3 534
后悔当初
后悔当初 2021-01-21 20:22

I have an array of longs I want to write to disk. The most efficient disk I/O functions take in byte arrays, for example:

FileOutputStream.write(by         


        
3条回答
  •  隐瞒了意图╮
    2021-01-21 20:50

    No, there is not a trivial way to convert from a long[] to a byte[].

    Your best option is likely to wrap your FileOutputStream with a BufferedOutputStream and then write out the individual byte values for each long (using bitwise operators).

    Another option is to create a ByteBuffer and put your long values into the ByteBuffer and then write that to a FileChannel. This handles the endianness conversion for you, but makes the buffering more complicated.

提交回复
热议问题