内存映射文件

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


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

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

你可能感兴趣的:(java,jdk,c,exception,String,buffer)