MappedByteBuffer 读取超过2G文件

16 篇文章 0 订阅
4 篇文章 0 订阅

 MappedByteBuffer处理大文件,一次只能读2G内容到内存中,为了读取大文件,需要循环读取处理。

protected void head() {
        //512m
        long length = 1L << 29;
        //4g
        long _4G = 1L << 32;
        long cur = 0L;
        try {
            MappedByteBuffer mappedByteBuffer;
            Random random = new Random();
            while (cur < _4G) {
                mappedByteBuffer = new RandomAccessFile("D:\\test\\bigfile.txt", "rw").getChannel()
                        .map(FileChannel.MapMode.READ_WRITE, cur, length);
                IntBuffer intBuffer = mappedByteBuffer.asIntBuffer();
                while (intBuffer.position() < intBuffer.capacity()) {
                    intBuffer.put(random.nextInt());
                }
                cur += length;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

需要注意的地方:移位运算(<<)

//这里的lL一定要带上L,不然是已int来计算的,结果会越界
long _4G = 1L << 32;
    @Override
    protected void head() {
        long _4g = 1<<32;
        //结果:1
        System.out.println(_4g);
        long _4G = 1L<<32;
        //结果:4294967296
        System.out.println(_4G);
    }

方法:java.nio.channels.FileChannel#map,这里的siez最大 = Integer.MAX_VALUE(2G)

    /*
     * @param  mode
     *         One of the constants {@link MapMode#READ_ONLY READ_ONLY}, {@link
     *         MapMode#READ_WRITE READ_WRITE}, or {@link MapMode#PRIVATE
     *         PRIVATE} defined in the {@link MapMode} class, according to
     *         whether the file is to be mapped read-only, read/write, or
     *         privately (copy-on-write), respectively
     *
     * @param  position
     *         The position within the file at which the mapped region
     *         is to start; must be non-negative
     *
     * @param  size
     *         The size of the region to be mapped; must be non-negative and
     *         no greater than {@link java.lang.Integer#MAX_VALUE}
     *
     * @return  The mapped byte buffer
     *
     * @throws NonReadableChannelException
     *         If the <tt>mode</tt> is {@link MapMode#READ_ONLY READ_ONLY} but
     *         this channel was not opened for reading
     *
     * @throws NonWritableChannelException
     *         If the <tt>mode</tt> is {@link MapMode#READ_WRITE READ_WRITE} or
     *         {@link MapMode#PRIVATE PRIVATE} but this channel was not opened
     *         for both reading and writing
     *
     * @throws IllegalArgumentException
     *         If the preconditions on the parameters do not hold
     *
     * @throws IOException
     *         If some other I/O error occurs
     *
     * @see java.nio.channels.FileChannel.MapMode
     * @see java.nio.MappedByteBuffer
     */
    public abstract MappedByteBuffer map(MapMode mode,
                                         long position, long size)
        throws IOException;

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
在Java中,可以使用 `MappedByteBuffer` 来实现随机读取文件。`MappedByteBuffer` 是 `ByteBuffer` 的子类,它通过将文件映射到内存中的字节缓冲区来实现对文件的随机访问。 下面是一个使用 `MappedByteBuffer` 随机读取文件的示例代码: ```java import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; public class FileRandomAccess { public static void main(String[] args) throws Exception { // 打开文件 RandomAccessFile file = new RandomAccessFile("path/to/file", "r"); // 获取文件通道 FileChannel channel = file.getChannel(); // 将文件映射到内存中 MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); // 随机读取文件内容 byte[] data = new byte[buffer.remaining()]; buffer.get(data); // 关闭文件通道和文件 channel.close(); file.close(); // 处理读取到的数据 System.out.println(new String(data)); } } ``` 上述代码中,首先通过 `RandomAccessFile` 打开文件,并获取其通道。然后使用 `FileChannel` 的 `map` 方法将文件映射到内存中的 `MappedByteBuffer` 对象。通过 `MappedByteBuffer` 对象可以直接读取文件内容,而无需进行显式的读取操作。最后,记得关闭文件通道和文件。 请注意替换示例代码中的 `"path/to/file"` 为实际的文件路径。另外,为了简化示例代码,这里将整个文件映射到内存中,你也可以根据需要仅映射文件的部分内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值