内存映射文件

JDK1.4版本引入了java.nio包,对文件流进行读写操作,提供无阻塞模式,同时也提供了一种高效率的文件读写模式,内存映射文件,把文件某个区域块映射到内存,进行高效率的读写,主要用到下面类

java.nio.MappedByteBuffer;
java.nio.channels.FileChannel

本文参考java核心技术的例子进行说明,代码如下:

 

package com.sohu.zxl;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.zip.CRC32;

public class FiteTest {

    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        String filename = "C:\\Program Files\\Java\\jdk1.6.0_10\\jre\\lib\\rt.jar";
        long crc = 0;
        long s1 = System.currentTimeMillis();
        crc = mapfile(filename);
        long e1 = System.currentTimeMillis();
        System.out.println("crc: "+crc+" time 1: "+(e1-s1));
        
        long s2 = System.currentTimeMillis();
        crc = randomfile(filename);
        long e2 = System.currentTimeMillis();
        System.out.println("crc: "+crc+" time 1: "+(e2-s2));
        
        long s3 = System.currentTimeMillis();
        crc = inputfile(filename);
        long e3 = System.currentTimeMillis();
        System.out.println("crc: "+crc+" time 1: "+(e3-s3));
        
        long s4 = System.currentTimeMillis();
        crc = bufferinputfile(filename);
        long e4 = System.currentTimeMillis();
        System.out.println("crc: "+crc+" time 1: "+(e4-s4));
    }

    /**
     * @throws FileNotFoundException
     * @throws IOException
     */
    private static long mapfile(String filename) throws FileNotFoundException, IOException {
        FileInputStream in = new FileInputStream(filename);
        FileChannel channel = in.getChannel();
        CRC32 crc = new CRC32();      //计算crc校验码,crc校验码可以检验文件内容是否一致
        long length = channel.size();
        //内存映射文件
        MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, length);
        for (int p=0; p<length; p++){
            int c = buffer.get(p);
            crc.update(c);
        }
        channel.close();
        return crc.getValue();
    }
    
    private static long randomfile(String filename) throws Exception{
        RandomAccessFile in = new RandomAccessFile(filename,"r");
        long length = in.length();
        CRC32 crc = new CRC32();
        for (long p=0; p<length; p++){
            in.seek(p);
            int c = in.readByte();
            crc.update(c);
        }
        in.close();
        return crc.getValue();
    }
    
    private static long inputfile(String filename) throws Exception{
        FileInputStream in = new FileInputStream(filename);
        CRC32 crc = new CRC32();
        int c;
        while((c=in.read())!= -1){
            crc.update(c);
        }
        in.close();
        return crc.getValue();
    }
    
    private static long bufferinputfile(String  filename) throws Exception{
        InputStream in = new BufferedInputStream(new FileInputStream(filename));
        CRC32 crc = new CRC32();
        int c;
        while((c=in.read())!= -1){
            crc.update(c);
        }
        in.close();
        return crc.getValue();
    }
}

执行结果如下:

crc: 78142897 time 1: 2109
crc: 78142897 time 1: 53845
crc: 78142897 time 1: 37454
crc: 78142897 time 1: 2172


从比对结果可以看出内存映射文件读写文件效率明显高于其他读写方式,其次是缓冲读写模式,随机最慢。

内存映射文件......................

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值