[NIO]利用内存映射文件以及普通通道进行文件拷贝效率分析

//测试代码, 用来验证内存映射文件、使用缓冲及流的文件读写的效率。可见内存映射文件的效率最高。
package com.nio.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class MappedFile {

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
	long start ;
	
	start = System.nanoTime();
	FileCopyBuffer("G:\\jdk-6u22-linux-i586.bin", "I:\\云相关2.rar");
	System.out.println((System.nanoTime() - start) / (float) 1000000000);
	start = System.nanoTime();
	FileCopyStream("G:\\btrace-bin.tar.gz", "I:\\云相关2.rar");
	System.out.println((System.nanoTime() - start) / (float) 1000000000);
	 start = System.nanoTime();
	FileCopyMaped("G:\\jdk-6u22-linux-i586.bin", "I:\\云相关2.rar");
	System.out.println((System.nanoTime() - start) / (float) 1000000000);
	
    }

    private static void FileCopyMaped(String source, String target) {

	FileInputStream in;
	FileOutputStream out;
	try {
	    in = new FileInputStream(source);
	    FileChannel inc = in.getChannel();
	    MappedByteBuffer buffer = inc.map(FileChannel.MapMode.READ_ONLY, 0,
		    inc.size());
	    out = new FileOutputStream(target);
	    FileChannel outc = out.getChannel();
	    outc.write(buffer);
	    inc.close();
	    outc.close();

	} catch (FileNotFoundException e) {
	    e.printStackTrace();
	} catch (IOException e) {
	    e.printStackTrace();
	}
    }

    private static void FileCopyBuffer(String source, String target) {

	FileInputStream in;
	FileOutputStream out;
	try {
	    in = new FileInputStream(source);
	    out = new FileOutputStream(target);
	    FileChannel inc = in.getChannel();
	    FileChannel outc = out.getChannel();
	    
	    ByteBuffer buffer = ByteBuffer.allocate(1024);
	    buffer.clear();
	    int tmp = inc.read(buffer);
	    while (tmp > 0) {
		buffer.flip();
		outc.write(buffer);
		buffer.clear();
		tmp = inc.read(buffer);
	    }
	    inc.close();
	    outc.close();

	} catch (FileNotFoundException e) {
	    e.printStackTrace();
	} catch (IOException e) {
	    e.printStackTrace();
	}
    }
    
    private static void FileCopyStream(String source, String target) {

	FileInputStream in;
	FileOutputStream out;
	try {
	    in = new FileInputStream(source);
	    out = new FileOutputStream(target);
	    int tmp = in.read();
	    while (tmp !=-1) {
		out.write(tmp);
		tmp = in.read();
	    }
	    out.flush();
	    in.close();
	    out.close();
	} catch (FileNotFoundException e) {
	    e.printStackTrace();
	} catch (IOException e) {
	    e.printStackTrace();
	}
    }
}

使用Buffer的拷贝文件大小:83761756,读写时间:1.3592848

使用流的拷贝文件大小:655656,读写时间:6.982578

使用内存映射文件拷贝文件大小:83761756,读写时间:1.0513209

可见使用内存映射文件的方式进行读写操作效率要高于Buffer的,而流操作是最耗时的(每次读写都要执行一次IO)。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值