Fastest way to incrementally read a large file

后端 未结 2 1824
灰色年华
灰色年华 2020-12-13 20:13

When given a buffer of MAX_BUFFER_SIZE, and a file that far exceeds it, how can one:

  1. Read the file in blocks of MAX_BUFFER_SIZE?
  2. Do it as fast as poss
2条回答
  •  悲&欢浪女
    2020-12-13 20:47

    If you want to make your first example faster

    FileChannel inChannel = new FileInputStream(fileName).getChannel();
    ByteBuffer buffer = ByteBuffer.allocateDirect(CAPACITY);
    
    while(inChannel.read(buffer) > 0)
        buffer.clear(); // do something with the data and clear/compact it.
    
    inChannel.close();
    

    If you want it to be even faster.

    FileChannel inChannel = new RandomAccessFile(fileName, "r").getChannel();
    MappedByteBuffer buffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
    // access the buffer as you wish.
    inChannel.close();
    

    This can take 10 - 20 micro-seconds for files up to 2 GB in size.

提交回复
热议问题