基于MappedByteBuffer & FileChannel 的大文件读取工具

博客围绕Java展开,但具体内容缺失。Java是后端开发常用语言,在众多项目中有广泛应用。

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

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 大文件读取工具
 **/
public class BigFileReader {

	private MappedByteBuffer[] mappedByteBuffers;
	private FileInputStream inputStream;
	private FileChannel fileChannel;

	private int bufferCountIndex = 0;
	private int bufferCount;

	private int byteBufferSize;
	private byte[] byteBuffer;

	public BigFileReader(String fileName, int byteBufferSize) throws IOException {
		this.inputStream = new FileInputStream(fileName);
		this.fileChannel = inputStream.getChannel();
		long fileSize = fileChannel.size();
		this.bufferCount = (int) Math.ceil((double) fileSize / (double) Integer.MAX_VALUE);
		this.mappedByteBuffers = new MappedByteBuffer[bufferCount];
		this.byteBufferSize = byteBufferSize;

		long preLength = 0;
		long regionSize = Integer.MAX_VALUE;
		for (int i = 0; i < bufferCount; i++) {
			if (fileSize - preLength < Integer.MAX_VALUE) {
				regionSize = fileSize - preLength;
			}
			mappedByteBuffers[i] = fileChannel.map(FileChannel.MapMode.READ_ONLY, preLength, regionSize);
			preLength += regionSize;
		}
	}

	public synchronized int read() {
		if (bufferCountIndex >= bufferCount) {
			return -1;
		}

		int limit = mappedByteBuffers[bufferCountIndex].limit();
		int position = mappedByteBuffers[bufferCountIndex].position();

		int realSize = byteBufferSize;
		if (limit - position < byteBufferSize) {
			realSize = limit - position;
		}
		byteBuffer = new byte[realSize];
		mappedByteBuffers[bufferCountIndex].get(byteBuffer);

		if (realSize < byteBufferSize && bufferCountIndex < bufferCount) {
			bufferCountIndex++;
		}
		return realSize;
	}

	public void close() throws IOException {
		fileChannel.close();
		inputStream.close();
		for (MappedByteBuffer byteBuffer: mappedByteBuffers) {
			byteBuffer.clear();
		}
		byteBuffer = null;
	}

	public synchronized byte[] getCurrentBytes() {
		return byteBuffer;
	}

	public static void main(String[] args) throws Exception {
		BigFileReader reader = new BigFileReader("文件路径", 1024);
		while (reader.read() != -1) {
			byte[] bytes = reader.getCurrentBytes();
			System.out.println(new String(bytes));
		}
		reader.close();
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值