Fastest way to read huge number of int from binary file

前端 未结 3 540
广开言路
广开言路 2020-12-21 01:09

I use Java 1.5 on an embedded Linux device and want to read a binary file with 2MB of int values. (now 4bytes Big Endian, but I can decide, the format)

Using D

3条回答
  •  一生所求
    2020-12-21 01:20

    I don't know if this will be any faster than what Alexander provided, but you could try mapping the file.

        try (FileInputStream stream = new FileInputStream(filename)) {
            FileChannel inChannel = stream.getChannel();
    
            ByteBuffer buffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
            int[] result = new int[500000];
    
            buffer.order( ByteOrder.BIG_ENDIAN );
            IntBuffer intBuffer = buffer.asIntBuffer( );
            intBuffer.get(result);
        }
    

提交回复
热议问题