exception while Read very large file > 300 MB

前端 未结 5 2076
时光取名叫无心
时光取名叫无心 2020-12-17 08:04

My task is to open a large file in READ&WRITE mode and i need to search some portion of text in that file by searching starting and end

5条回答
  •  遥遥无期
    2020-12-17 08:49

    Claims that the FileChannel.map will load the entire file into memory are faulty, with reference to the MappedByteBuffer that FileChannel.map() returns. It is a 'Direct Byte Buffer', it not exhaust your memory (Direct Byte Buffers use the OS virtual memory subsystem to page data in and out of memory as required, allowing one to address much larger chunks of memory as they were physical RAM.) But then again, a single MBB will only work for files to ~2GB.

    Try this:

    FileChannel fc = new FileInputStream(fFile).getChannel();
    MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
    
    CharBuffer chrBuff = mbb.asCharBuffer();
    

    It will not load the entire file into memory, and the chrBuff is only a view of the backing MappedByteBuffer, and not a copy.

    I'm not sure how to handle the decoding, though.

提交回复
热议问题