Java的NIO实例——基于NIO的文件复制

22 篇文章 0 订阅

JDK1.4中引入的NIO库,和传统IO最大的区别是:传统IO是基于Byte(字节)和Stream()的,而NIO是基于Buffer(缓冲)、Channel(通道)和Selector(选择器)的。下面就看下使用IO,BufferedIO和NIO分别实现的文件复制(本来想测试一下分别的性能,但是结果不是很稳定,希望有这方面数据的同学分享下):

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.Flushable;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileUtil {
	public static void main(String args[]){
		
		String srcFilePath = "E:/solr-4.1.0.zip";
		String destFilePath = "E:/0.zip";
		String destFilePath1 = "E:/1.zip";
		String destFilePath2 = "E:/2.zip";
		
		File srcFile = new File(srcFilePath);
		File destFile = new File(destFilePath);
		File destFile1 = new File(destFilePath1);
		File destFile2 = new File(destFilePath2);

		long start = System.currentTimeMillis();
		for(int i=0;i<10;i++){
			copyFileByNIO(srcFile, new File("E:/"+i+".zip"));
		}
		long end = System.currentTimeMillis();
		System.out.println("All times="+(end-start));
		
		/*copyFileByNIO(srcFile, destFile2);
		copyFileByIO(srcFile, destFile);
		copyFileByBufferedIO(srcFile, destFile1);*/
	}
	
	
	
	public static void copyFileByNIO(File srcFile, File destFile){
		long start = System.currentTimeMillis();
		FileInputStream in = null;
		FileOutputStream out = null;
		FileChannel fcin = null;
		FileChannel fcout = null;
		try{
			in = new FileInputStream(srcFile);
			out = new FileOutputStream(destFile);
			
			fcin = in.getChannel();
			fcout = out.getChannel();
			ByteBuffer buffer = ByteBuffer.allocate(1024);
			
			while(fcin.read(buffer)!=-1){
				buffer.flip();
				fcout.write(buffer);
				buffer.clear();
			}
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			close(in);
			close(out);
			close(fcin);
			close(fcout);
			long end = System.currentTimeMillis();
			System.out.println("copyFileByNIO: time="+(end-start));
		}
	}
	
	public static void copyFileByIO(File srcFile, File destFile){
		long start = System.currentTimeMillis();
		FileInputStream is = null;
		FileOutputStream os = null;
		try{
			is =new FileInputStream(srcFile);
			os = new FileOutputStream(destFile);
			byte[] bytes = new byte[1024];
			int count=0;
			while((count=is.read(bytes)) != -1){
				os.write(bytes, 0, count);
			}
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			close(is);
			close(os);
			long end = System.currentTimeMillis();
			System.out.println("copyFileByIO: time="+(end-start));
		}
	}
	
	public static void copyFileByBufferedIO(File srcFile, File destFile){
		long start = System.currentTimeMillis();
		BufferedInputStream is = null;
		BufferedOutputStream os = null;
		try{
			is =new BufferedInputStream(new FileInputStream(srcFile));
			os = new BufferedOutputStream(new FileOutputStream(destFile));
			byte[] bytes = new byte[1024];
			int count=0;
			while((count=is.read(bytes)) != -1){
				os.write(bytes, 0, count);
			}
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			close(is);
			close(os);
			long end = System.currentTimeMillis();
			System.out.println("copyFileByBufferedIO: time="+(end-start));
		}
	}
	
	
	public static void close(Closeable device){
		if(device != null){
			try{
				if(device instanceof Flushable){
					((Flushable) device).flush();
				}
				device.close();
			}catch(IOException e){
				
			}
		}
	}
	
	
	private FileUtil(){}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值