java_croe 学习笔记之新IO---java.nio 之内存映射文件

本文通过对比不同文件读取方式的性能,展示了内存映射文件作为一种高效文件处理技术的优势。通过对同一文件使用无格式输入流、缓冲输入流、随机存取文件及内存映射文件进行读取并计算校验和,结果显示内存映射文件方式最为快速。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

解释:将文件的一段区域映射到内存中,比传统的文件处理速度要快很多

 

参考:

无格式输入流 110秒

缓冲输入流     9.9秒

随机存取文件  162秒

内存映射文件   7.2秒

 

例子

package twelve;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.zip.CRC32;

/**
  @Title NIOTTest.java
  @description  TODO
  @author qinpeng
  @date Aug 25, 2009 10:23:26 PM
 */
public class NIOTTest {
	
	public static void main(String[] args) {
		
		String fileName = "d:\\IOTest.pdf";
		
		System.out.println("inputStream");
		long start = System.currentTimeMillis();
		long crcValue = checksumInputStreanm(fileName);
		long end = System.currentTimeMillis();
		System.out.println(Long.toHexString(crcValue));
		System.out.println((end - start)+"耗时");
		
		System.out.println("BufferedinputStream");
		start = System.currentTimeMillis();
	    crcValue = checksumInputStreanm(fileName);
	    end = System.currentTimeMillis();
		System.out.println(Long.toHexString(crcValue));
		System.out.println((end - start)+"耗时");
		
		System.out.println("RandomAccessFileinputStream");
		start = System.currentTimeMillis();
		crcValue = checksumInputStreanm(fileName);
		end = System.currentTimeMillis();
		System.out.println(Long.toHexString(crcValue));
		System.out.println((end - start)+"耗时");
		
		System.out.println(" MappedFile inputStream");
		start = System.currentTimeMillis();
		crcValue = checksumInputStreanm(fileName);
		end = System.currentTimeMillis();
		System.out.println(Long.toHexString(crcValue));
		System.out.println((end - start)+"耗时");
	}
	
	
	
	public static long checksumInputStreanm(String fileName){
		CRC32 crc = new CRC32();
		try {
			InputStream in = new FileInputStream(fileName);
			int c;
			while((c=in.read())!=-1){
				crc.update(c);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.err.print("NIOTTest--checksumInputStreanm--new FileInputStream is not found");
		} catch(IOException ioe){
			ioe.printStackTrace();
			System.err.print("NIOTTest--checksumInputStreanm--new FileInputStream'read append IOException");
		}
		return crc.getValue();
	}
	
	public static long checksumBufferedInputStream(String fileName){
		CRC32 crc = new CRC32();
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(fileName));
			int c;
			while((c=in.read())!=-1){
				crc.update(c);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.err.print("NIOTTest--checksumBufferedInputStream--new FileInputStream is not found");
		} catch(IOException ioe){
			ioe.printStackTrace();
			System.err.print("NIOTTest--checksumBufferedInputStream--new FileInputStream'read append IOException");
		}
		return crc.getValue();
	}
	
	
	public static long checksumRondomAccessFileInputStream(String fileName){
		CRC32 crc = new CRC32();
		try {
			RandomAccessFile file = new RandomAccessFile(fileName,"r");
			int c;
			while((c=file.read())!=-1){
				crc.update(c);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.err.print("NIOTTest--checksumRondomAccessFileInputStream--new FileInputStream is not found");
		} catch(IOException ioe){
			ioe.printStackTrace();
			System.err.print("NIOTTest--checksumRondomAccessFileInputStream--new FileInputStream'read append IOException");
		}
		return crc.getValue();
	}

	public static long checksumMappedFile(String fileName){
		CRC32 crc = new CRC32();
		try {
			FileInputStream in = new FileInputStream(fileName);
			FileChannel channel = in.getChannel();
			int length = (int) channel.size();
			MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, length);
			
			for(int p = 0;p<length;p++){
				int c = buffer.getInt(p);
				crc.update(c);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.err.print("NIOTTest--checksumRondomAccessFileInputStream--new FileInputStream is not found");
		} catch(IOException ioe){
			ioe.printStackTrace();
			System.err.print("NIOTTest--checksumRondomAccessFileInputStream--new FileInputStream'read append IOException");
		}
		return crc.getValue();
	}
	
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值