通过内存映射读取超大文件 --《JAVA编程思想》82

假设文件太大的话,我们不能将整个文件都读取到内存之中,但可以通过内存映射文件,每次将文件的一小部分映射到内存中,即使再大的文件,也可以对其修改。

下面一起来看个小例子:

public class LargeMappedFiles {
	
    static int length = 128 * 1024 * 1024;

    public static void main(String[] args) throws IOException {
        MappedByteBuffer out =
                new RandomAccessFile("D:\\test\\bigFile.txt", "rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0,
                        length);
        //覆写映射区域内的每个字节
        for (int i = 0; i < length; i++) {
            out.put((byte) 'x');
        }
        System.out.println("Finished writing");
        //读取映射区域中间的字节
        for (int i = length / 2; i < length / 2 + 6; i++) {
            System.out.println((char) out.get(i));
        }
    }
    
}
Finished writing
x
x
x
x
x
x

为了演示同时进行读写操作,选用了 RandomAccessFile ,getChannel() 获取该文件的通道,然后调用 map() 产生 MappedByteBuffer,此时必须指定的参数为:读写类型、映射起始位置、映射区域长度。

MappedByteBuffer 是一种特殊类型的直读缓冲器,它继承自 ByteBuffer ,包含 ByteBuffer 中的所有方法。

MappedByteBuffer 属于 nio 类,虽然旧的 I/O 类也在用 nio 重新实现过后性能有所提高,但通过使用内存映射文件,可以大幅提示性能。

下面我们通过一个测试案例来比较两者的差异。

public class MappedIO {

    private static int numOfInts = 4000000;

    private static int numOfUbffInts = 200000;

    private static String file = "D:\\test\\test.txt";

    private abstract static class Tester {

        private String name;

        public Tester(String name) {
            this.name = name;
        }

        public abstract void test() throws IOException;

        public void runTest() {
            System.out.print(name + ":");
            //统计执行test方法的时间
            long start = System.nanoTime();
            try {
                test();
            } catch (IOException e) {
                e.printStackTrace();
            }
            double duration = System.nanoTime() - start;
            //将纳秒转换为秒,1秒 = 10的9次方纳秒
            System.out.format("%.2f\n", duration / 1.0e9);
        }
    }

    private static Tester[] tests = {
            new Tester("Stream Write") {
                @Override
                public void test() throws IOException {
                    DataOutputStream dos =
                            new DataOutputStream(new BufferedOutputStream(new FileOutputStream(new File(file))));
                    for (int i = 0; i < numOfInts; i++) {
                        dos.writeInt(i);
                    }
                    dos.close();
                }
            },
            new Tester("Mapped Write") {
                @Override
                public void test() throws IOException {
                    FileChannel fc = new RandomAccessFile(file, "rw").getChannel();
                    IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size()).asIntBuffer();
                    for (int i = 0; i < numOfInts; i++) {
                        ib.put(i);
                    }
                    fc.close();
                }
            },
            new Tester("Stream Read") {
                @Override
                public void test() throws IOException {
                    DataInputStream dis =
                            new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
                    for (int i = 0; i < numOfInts; i++) {
                        dis.readInt();
                    }
                    dis.close();
                }
            },
            new Tester("Mapped Read") {
                @Override
                public void test() throws IOException {
                    FileChannel fc = new FileInputStream(new File(file)).getChannel();
                    IntBuffer ib = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()).asIntBuffer();
                    while (ib.hasRemaining()) {
                        ib.get();
                    }
                    fc.close();
                }
            },
            new Tester("Stream Read/Write") {
                @Override
                public void test() throws IOException {
                    RandomAccessFile raf = new RandomAccessFile(new File(file), "rw");
                    raf.write(1);
                    for (int i = 0; i < numOfUbffInts; i++) {
                        //移动指针,后退4个字节(int类型占4个字节)
                        raf.seek(raf.length() - 4);
                        //将前一个int类型写入
                        raf.writeInt(raf.readInt());
                    }
                    raf.close();
                }
            },
            new Tester("Mapped Read/Write") {
                @Override
                public void test() throws IOException {
                    FileChannel fc = new RandomAccessFile(new File(file), "rw").getChannel();
                    IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size()).asIntBuffer();
                    ib.put(0);
                    for (int i = 1; i < numOfUbffInts; i++) {
                        //每次都读取前一个int类型进行写入
                        ib.put(ib.get(i - 1));
                    }
                    fc.close();
                }
            }
    };

    public static void main(String[] args) {
        for (Tester test : tests) {
            test.runTest();
        }
    }
    
}
Stream Write:0.15
Mapped Write:0.03
Stream Read:0.24
Mapped Read:0.01
Stream Read/Write:7.16
Mapped Read/Write:0.01

runTest() 被用作为模板方法,每种子类都执行相同的测试,提供了 I/O 操作的原型。

需要注意的是,Mapped Write 测试使用的是 RandomAccessFile 而不是 FileOutputStream ,因为进行内存文件映射必须进行读取操作,故要使用能同时进行读写的 RandomAccessFile 。

通过对传统的 I/O 类和使用内存映射文件进行比较,在读、写、读写各个方面来说,内存映射文件的效率显著高于传统 I/O 类。


本次分享至此结束,希望本文对你有所帮助,若能点亮下方的点赞按钮,在下感激不尽,谢谢您的【精神支持】。

若有任何疑问,也欢迎与我交流,若存在不足之处,也欢迎各位指正!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BaymaxCS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值