几种文件读写方式对比

文件读写方式及其对比

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 文件复制
 * @author 48450
 *
 */
public class Filecopy {

	public static void main(String[] args) throws IOException {
		String sourcePath="C:\\Users\\48450\\Desktop\\test\\source\\org.rar";
		String targetPath="C:\\Users\\48450\\Desktop\\test\\target\\";
		long[] time=new long[10];
		int average=0;
		for (int i = 0; i < time.length; i++) {
			long start=System.currentTimeMillis();
			String tempTargetPath=targetPath+"org"+i+".rar";
			Filecopy.traditionalCopy(sourcePath, tempTargetPath);
//			Filecopy.bufferCopy(sourcePath, tempTargetPath);
//			Filecopy.nioCopy(sourcePath, tempTargetPath);
//			Filecopy.nioCopy2(sourcePath, tempTargetPath);
			long end=System.currentTimeMillis();
			time[i]=end-start;
			average+=time[i];
		}
		System.out.println(average/10.0);
	}
	
	
	/**
	 * 传统文件读写+缓存方式复制文件
	 * @param sourcePath
	 * @param targetPath
	 * @throws IOException
	 */
	public static void traditionalCopy(String sourcePath,String targetPath) throws IOException {
		File source=new File(sourcePath);
		if (!source.exists()) {
			source.createNewFile();
		}
		File target=new File(targetPath);
		if (!target.exists()) {
			target.createNewFile();
		}
		FileInputStream fis=new FileInputStream(source);
		FileOutputStream fos=new FileOutputStream(target);
		byte[] buf=new byte[128];
		int len=0;
		while ((len=fis.read(buf))!=-1) {
			fos.write(buf,0,len);
		}
		fis.close();
		fos.close();
	}
	
	/**
	 * BUfferIo缓存方式复制文件
	 * @param sourcePath
	 * @param targetPath
	 * @throws IOException
	 */
	public static void bufferCopy(String sourcePath,String targetPath) throws IOException {
		File source=new File(sourcePath);
		if (!source.exists()) {
			source.createNewFile();
		}
		File target=new File(targetPath);
		if (!target.exists()) {
			target.createNewFile();
		}
		BufferedInputStream fis=new BufferedInputStream(new FileInputStream(source));
		BufferedOutputStream fos=new BufferedOutputStream(new FileOutputStream(target));
		int len=0;
		while ((len=fis.read())!=-1) {
			fos.write((byte)len);
		}
		fis.close();
		fos.close();
	}
	
	/**
	 * nio文件通道方式复制文件
	 * @param sourcePath
	 * @param targetPath
	 * @throws IOException
	 */
	public static void nioCopy(String sourcePath,String targetPath) throws IOException {
		File source=new File(sourcePath);
		if (!source.exists()) {
			source.createNewFile();
		}
		File target=new File(targetPath);
		if (!target.exists()) {
			target.createNewFile();
		}
		FileInputStream fis=new FileInputStream(source);
		FileOutputStream fos=new FileOutputStream(target);
		FileChannel sourceChannel=fis.getChannel();
		FileChannel targetChannel=fos.getChannel();
		targetChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
		sourceChannel.close();
		targetChannel.close();
		fis.close();
		fos.close();
	}

	/**
	 * 文件内存映射及件通道方式复制文件
	 * @param sourcePath
	 * @param targetPath
	 * @throws IOException
	 */
	public static void nioCopy2(String sourcePath,String targetPath) throws IOException {
		File source=new File(sourcePath);
		if (!source.exists()) {
			source.createNewFile();
		}
		File target=new File(targetPath);
		if (!target.exists()) {
			target.createNewFile();
		}
		FileInputStream fis=new FileInputStream(source);
		FileOutputStream fos=new FileOutputStream(target);
		FileChannel sourceChannel=fis.getChannel();
		FileChannel targetChannel=fos.getChannel();
		MappedByteBuffer mappedByteBuffer=sourceChannel.map(FileChannel.MapMode.READ_ONLY, 0, sourceChannel.size());
		targetChannel.write(mappedByteBuffer);
		sourceChannel.close();
		targetChannel.close();
		fis.close();
		fos.close();
	}

}

测试的文件是156MB的压缩文件,测试复制十遍的平均花费时间。
测试速率对比(单位ms)
                      传统缓存 BufferIO NIO文件通道 文件内存映射+通道
第一次测试:10290.4   4399.9   3729.0           6837.0
第二次测试:10639.0   4481.0   3875.1           8146.9

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值