1. 程式人生 > >Java NIO和BIO讀寫簡單比較

Java NIO和BIO讀寫簡單比較

通過對檔案的讀寫所花的時間來進行比較。

一、讀寫的檔案

二、例項程式碼

package nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class NIOVSIO
{
	public static void readIO() throws Exception{
		File file = new File("E:/迅雷下載/美人魚.HD.720p.國語中字.rmvb");
		File file2 = new File("E:/迅雷下載/美人魚.HD.720p.國語中字io.rmvb");
		FileInputStream fis = new FileInputStream(file);
		FileOutputStream fos = new FileOutputStream(file2);
		byte[] buffer = new byte[1024];
		int len = -1;
		while((len = fis.read(buffer))!= -1){
			fos.write(buffer, 0, len);
		}
		fis.close();
		fos.close();
	}
	public static void readNIO() throws Exception{
		File file = new File("E:/迅雷下載/美人魚.HD.720p.國語中字.rmvb");
		File file3 = new File("E:/迅雷下載/美人魚.HD.720p.國語中字nio.rmvb");
		FileInputStream fis = new FileInputStream(file);
		FileOutputStream fos = new FileOutputStream(file3);
		FileChannel channel = fis.getChannel();
		FileChannel outchannel = fos.getChannel();
		ByteBuffer buffer = ByteBuffer.allocate(1024);
		while(channel.read(buffer)!=-1){
			buffer.flip();
		    outchannel.write(buffer);
			buffer.clear();
		}
		channel.close(); 
		fis.close();
		fos.close();
	}
	public static void main(String[] args) throws Exception
	{
		
		long nioStart = System.currentTimeMillis();
		readNIO();
		System.out.println("nio-time: "+((System.currentTimeMillis()-nioStart)));
		long ioStart = System.currentTimeMillis();
		readIO();
		System.out.println("io-time: "+((System.currentTimeMillis()-ioStart)));
	}
}

三次測試的結果: