/*
* @param filePath 文件路径
* @param bufferSize 缓冲区大小 e.g. 1024 * 1024 * 8=8M
* @throws Exception
*/
public static void memoryMappedRead(String filePath,int bufferSize) throws Exception {
File file = new File(filePath);
long length = file.length();
/**
* 获取系统的换行符, windows 为“\r\n”
*/
String enterStr = System.getProperty(“line.separator”, “\n”);
int lengthOfEnterStr = enterStr.length();
String preciousLastPartString = “”;
String currentFirstPartString = “”;
@SuppressWarnings(“resource”)
MappedByteBuffer mbb = new RandomAccessFile(filePath, “r”).getChannel()
.map(FileChannel.MapMode.READ_ONLY, 0, length);
StringBuilder strBuf = new StringBuilder("");
long begin = System.currentTimeMillis();
long cycle = length / bufferSize;
int mode = (int) (length % bufferSize);
boolean partStringFlag = false;
for (int i = 0; i < cycle; i++) {
byte[] bytes = new byte[bu

该博客介绍了一个使用MappedByteBuffer从文件中读取内容的方法,通过指定文件路径和缓冲区大小,实现高效的数据读取。代码示例展示了如何创建MappedByteBuffer,以及处理文件中的换行符。
最低0.47元/天 解锁文章
341

被折叠的 条评论
为什么被折叠?



