java、mappedByteBuffer读取文件
2015-07-22 23:00
351 查看
/*
* @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);
* @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[bufferSize]; mbb.get(bytes); String tempString = new String(bytes, 0, bufferSize); int fromIndex = 0, endIndex = 0; if (partStringFlag) { endIndex = tempString.indexOf(enterStr, fromIndex); if (endIndex == 0) { // 当前模块的开始为换行符,i.e. 前一块数据刚好到结尾,但是不包含换行符 currentFirstPartString = ""; } else if (endIndex > 0) { currentFirstPartString = tempString.substring(fromIndex, endIndex); } String totalLine = preciousLastPartString + currentFirstPartString; System.out.println("las part: " + preciousLastPartString + "---->" + "currentPart: " + currentFirstPartString); System.out.println("conbined line: " + totalLine); fromIndex = endIndex + lengthOfEnterStr; while ((endIndex = tempString.indexOf(enterStr, fromIndex)) != -1) { String line = tempString.substring(fromIndex, endIndex); line = new String(strBuf.toString() + line); strBuf.delete(0, strBuf.length()); fromIndex = endIndex + lengthOfEnterStr; } if (endIndex == -1) { preciousLastPartString = tempString.substring(fromIndex, tempString.length()); partStringFlag = true; } else { partStringFlag = false; } } else { while ((endIndex = tempString.indexOf(enterStr, fromIndex)) != -1) { String line = tempString.substring(fromIndex, endIndex); line = new String(strBuf.toString() + line); strBuf.delete(0, strBuf.length()); fromIndex = endIndex + lengthOfEnterStr; } if ((endIndex == -1) && (fromIndex < bufferSize)) { preciousLastPartString = tempString.substring(fromIndex, tempString.length()); partStringFlag = true; } else { partStringFlag = false; } } } if (mode > 0) { byte[] bytes = new byte[mode]; mbb.get(bytes); String tempString = new String(bytes, 0, mode); int fromIndex = 0, endIndex = 0; if (partStringFlag) { endIndex = tempString.indexOf(enterStr, fromIndex); currentFirstPartString = tempString.substring(fromIndex, endIndex); String totalLine = preciousLastPartString + currentFirstPartString; System.out.println("mode combined line: " + totalLine); fromIndex = endIndex + lengthOfEnterStr; while ((endIndex = tempString.indexOf(enterStr, fromIndex)) != -1) { String line = tempString.substring(fromIndex, endIndex); line = new String(strBuf.toString() + line); System.out.println(line); strBuf.delete(0, strBuf.length()); fromIndex = endIndex + lengthOfEnterStr; } } else { while ((endIndex = tempString.indexOf(enterStr, fromIndex)) != -1) { String line = tempString.substring(fromIndex, endIndex); line = new String(strBuf.toString() + line); strBuf.delete(0, strBuf.length()); fromIndex = endIndex + lengthOfEnterStr; } } } long end = System.currentTimeMillis(); System.out .println("total time: " + (double) (end - begin) / 1000 + "s"); }
相关文章推荐
- Android布局(二)RelativeLayout
- 手机移动端js插件手指缩放图片
- 对android rotation的添加与定制
- Cocos2d-x制作跳棋结束总结
- iOS开发中可能有用的那些分类们Categories
- 简单谈谈对Object C的理解
- 关于在Unity中启用外部的exe
- 我在iOS开发工程师的路上正在学习
- 移动端 图片不变形
- android如何阻止app_name在标题栏中闪现
- android shape的使用详解以及常用效果(渐变色、分割线、边框、半透明阴影效果等)
- Android 的进程与线程总结
- Android布局(一)LinearLayout
- 【进阶android】ListView源码分析——子视图的七种填充方式
- CellularAutomation(细胞自动机)
- Asp.mvc(二)~使用AutoMapper实现领域模型与DTO映射
- Android开发之SoundPool使用详解
- ios7关于图片资源使用需要注意的问题
- 【攻克Android (6)】事件
- 《慕客网:IOS基础入门之Foundation框架初体验》学习笔记 <五> NSDicionary + NSMutableDictionary