您的位置:首页 > 移动开发

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);

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");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: