Java IO和NIO讀取大文件,對比速度

話題:假設機器只有500M內存,有一個1.23GB的文件,要從一個目錄複製到另外一個目錄
目的:比較IO與NIO的讀取速度效率
細節:大文件不能一次讀到內存中,否則會內存溢出,只能每次讀取固定大小的數據流

在這裏插入圖片描述
下面進行代碼實現,在實現代碼中,有的代碼是一次性讀取全部內容到內存中,有的是讀取固定大小,分別看看這些方法讀取文件速度的差異

文件大小 1.23GB
1.使用RandomAccessFile讀取文件,FileOutputStream寫文件,耗時:8768ms
2.使用BufferedInputStream讀取文件,BufferedOutputStream寫文件,耗時:2202ms
3.使用Scanner讀文件,FileOutputStream寫文件,耗時:120945ms
4.使用NIO,FileChannel讀寫文件,耗時:

  • NIO每次讀取1M,耗時:8947ms
  • NIO每次讀取5M,耗時:2976ms
  • NIO每次讀取10M,耗時:1802ms
  • NIO每次讀取20M,耗時:1279ms

基本上使用NIO和有緩衝區的IO–BufferedInputStream讀寫文件速度最快,通過任務管理器觀察對內存的使用基本在100M上下浮動
在這裏插入圖片描述

package com.zypcy.readbigfile;

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.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Scanner;

/**
 * 讀取大文件
 * 分別測試不同文件流IO與NIO讀取的效率
 * 因文件很大,機器可用內存較小,無法一次讀取全部內容,需要讀取固定大小的數據
 * @author zhuyu
 */
public class Demo {

	public static void main(String[] args) {
		String filePath = "D:\\project\\java\\workspace\\DemoThread\\src\\com\\zypcy\\readbigfile\\bigdata.zip";
		String newPath = "D:\\project\\java\\workspace\\DemoThread\\src\\com\\zypcy\\readbigfile\\output.zip";
		
		try {
			File file = new File(filePath);
			if(!file.exists()){
				return;
			}
			File newFile = new File(newPath);
			
			//randomAccessRead(file ,newFile); 
			//測試:讀取1.23GB文件耗時:8768ms
			
			//bufferedRead(file ,newFile);
			//測試:讀取1.23GB文件耗時:2202ms
			
			//scannerRead(file ,newFile);
			//測試:讀取1.23GB文件耗時:120945ms
			
			fileChannelRead(file ,newFile);
			//每次讀取1M,耗時:8947ms
			//每次讀取5M,耗時:2976ms
			//每次讀取10M,耗時:1802ms
			//每次讀取20M,耗時:1279ms
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 使用 RandomAccessFile 讀取文件
	 * @param file
	 * @param newFile
	 */
	public static void randomAccessRead(File file , File newFile){
		//測試:讀取1.23GB文件耗時:8768ms
		long d1 = System.currentTimeMillis();
		RandomAccessFile raf = null;
		OutputStream output = null;
		try {
			raf = new RandomAccessFile(file , "rw");
			output = new FileOutputStream(newFile);
			int len = 0;             //每次讀取內容長度
			byte[] data = new byte[1024];//內容緩衝區
			while((len = raf.read(data)) != -1){
				output.write(data, 0, len);
			}
			long d2 = System.currentTimeMillis();
			System.out.println("randomAccessRead讀取完成,耗時:" + (d2 - d1));
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(raf != null){
					raf.close();
				}
				if(output != null){
					output.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 使用NIO的FileChannel讀取
	 * @param file
	 * @param newFile
	 */
	public static void fileChannelRead(File file , File newFile){
		//1.23GB文件
		//每次讀取1M,耗時:8947ms
		//每次讀取5M,耗時:2976ms
		//每次讀取10M,耗時:1802ms
		//每次讀取20M,耗時:1279ms
		long d1 = System.currentTimeMillis();
		FileInputStream in = null;
		FileOutputStream output = null;
		FileChannel  fic = null;
		FileChannel  foc = null;
		try {
			in = new FileInputStream(file);
			output = new FileOutputStream(newFile);
			fic = in.getChannel();
			foc = output.getChannel();
			
			//fic.transferTo(0, fic.size(), foc);
			ByteBuffer buf = ByteBuffer.allocate(20480);
			while(fic.read(buf) != -1){
				buf.flip();//切換到讀取數據模式
				foc.write(buf);//將緩衝區的數據寫入通道中
				buf.clear();//清空緩衝區
			}
			
			long d2 = System.currentTimeMillis();
			System.out.println("fileChannelRead讀取完成,耗時:" + (d2 - d1));
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
				try {
					if(in != null){
						in.close();
					}
					if(output != null){
						output.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
	
	/**
	 * 使用IO的緩衝區讀取
	 * @param file
	 * @param newFile
	 */
	public static void bufferedRead(File file , File newFile){
		//測試:讀取1.23GB文件耗時:2202
		long d1 = System.currentTimeMillis();
		InputStream in = null;
		OutputStream output = null;
		try {
			in = new BufferedInputStream(new FileInputStream(file)) ;
			output = new BufferedOutputStream(new FileOutputStream(newFile));
			int len = 0;
			byte[] data = new byte[1024];
			while((len = in.read(data)) != -1){
				output.write(data, 0, len);
			}
			long d2 = System.currentTimeMillis();
			System.out.println("bufferedRead讀取完成,耗時:" + (d2 - d1));
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
				try {
					if(in != null){
						in.close();
					}
					if(output != null){
						output.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
	
	/**
	 * 使用Scanner一行一行讀取
	 * @param file
	 * @param newFile
	 */
	public static void scannerRead(File file , File newFile){
		//讀取1.23GB文件耗時:120945
		long d1 = System.currentTimeMillis();
		InputStream in = null;
		OutputStream output = null;
		try {
			in = new FileInputStream(file);
			output = new FileOutputStream(newFile);
			Scanner sc = new Scanner(in, "UTF-8");
			//sc.useDelimiter("\\r\\n");
			while(sc.hasNext()){
				String content = sc.nextLine();
				output.write(content.getBytes("UTF-8"));
			}
			long d2 = System.currentTimeMillis();
			System.out.println("scannerRead讀取完成,耗時:" + (d2 - d1));
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
				try {
					if(in != null){
						in.close();
					}
					if(output != null){
						output.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章