资源简介
对大数据文本文件读取(按行读取)的优化,目前常规的方案有三种,第一种LineNumberReader,第二种RandomAccessFile,第三种是内存映射文件在RandomAccessFile基础上调用getChannel().map(...);代码提供在RandomAccessFile基础上,整合内部缓冲区,效率会有提高,测试过程中1000w行数据用时1秒,1亿行数据用时103(比1438秒快了13倍左右)

代码片段和文件信息
package com.gqshao.file.io;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;
public class BufferedRandomAccessFile extends RandomAccessFile {
static final int LogBuffSz_ = 16; // 64K buffer
public static final int BuffSz_ = (1 << LogBuffSz_);
static final long BuffMask_ = ~(((long) BuffSz_) - 1L);
private String path_;
/*
* This implementation is based on the buffer implementation in Modula-3‘s
* “Rd“ “Wr“ “RdClass“ and “WrClass“ interfaces.
*/
private boolean dirty_; // true iff unflushed bytes exist
private boolean syncNeeded_; // dirty_ can be cleared by e.g. seek so track sync separately
private long curr_; // current position in file
private long lo_ hi_; // bounds on characters in “buff“
private byte[] buff_; // local buffer
private long maxHi_; // this.lo + this.buff.length
private boolean hitEOF_; // buffer contains last file block?
private long diskPos_; // disk position
/*
* To describe the above fields we introduce the following abstractions for
* the file “f“:
*
* len(f) the length of the file curr(f) the current position in the file
* c(f) the abstract contents of the file disk(f) the contents of f‘s
* backing disk file closed(f) true iff the file is closed
*
* “curr(f)“ is an index in the closed interval [0 len(f)]. “c(f)“ is a
* character sequence of length “len(f)“. “c(f)“ and “disk(f)“ may differ if
* “c(f)“ contains unflushed writes not reflected in “disk(f)“. The flush
* operation has the effect of making “disk(f)“ identical to “c(f)“.
*
* A file is said to be *valid* if the following conditions hold:
*
* V1. The “closed“ and “curr“ fields are correct:
*
* f.closed == closed(f) f.curr == curr(f)
*
* V2. The current position is either contained in the buffer or just past
* the buffer:
*
* f.lo <= f.curr <= f.hi
*
* V3. Any (possibly) unflushed characters are stored in “f.buff“:
*
* (forall i in [f.lo f.curr): c(f)[i] == f.buff[i - f.lo])
*
* V4. For all characters not covered by V3 c(f) and disk(f) agree:
*
* (forall i in [f.lo len(f)): i not in [f.lo f.curr) => c(f)[i] ==
* disk(f)[i])
*
* V5. “f.dirty“ is true iff the buffer contains bytes that should be
* flushed to the file; by V3 and V4 only part of the buffer can be dirty.
*
* f.dirty == (exists i in [f.lo f.curr): c(f)[i] != f.buff[i - f.lo])
*
* V6. this.maxHi == this.lo + this.buff.length
*
* Note that “f.buff“ can be “null“ in a valid file since the range of
* characters in V3 is empty when “f.lo == f.curr“.
*
* A file is said to be *ready* if the buffer contai
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3915 2016-01-17 20:31 FileUtil.java
文件 11624 2016-01-17 20:29 BufferedRandomAccessFile.java
- 上一篇:serializer.jar
- 下一篇:android 蓝牙SPP传输demo
相关资源
- 微博系统(Java源码,servlet+jsp),适
- java串口通信全套完整代码-导入eclip
- jsonarray所必需的6个jar包.rar
- 三角网构TIN生成算法,Java语言实现
- java代码编写将excel数据导入到mysql数据
- Java写的cmm词法分析器源代码及javacc学
- JAVA JSP公司财务管理系统 源代码 论文
- JSP+MYSQL旅行社管理信息系统
- 推荐算法的JAVA实现
- 基于Java的酒店管理系统源码(毕业设
- java-图片识别 图片比较
- android毕业设计
- java23种设计模式+23个实例demo
- java Socket发送/接受报文
- JAVA828436
- java界面美化 提供多套皮肤直接使用
- 在线聊天系统(java代码)
- 基于Java的图书管理系统807185
- java中实现将页面数据导入Excel中
- java 企业销售管理系统
- java做的聊天系统(包括正规课程设计
- Java编写的qq聊天室
- 商店商品管理系统 JAVA写的 有界面
- JAVA开发聊天室程序
- 在linux系统下用java执行系统命令实例
- java期末考试试题两套(答案) 选择(
- JAVA3D编程示例(建模、交互)
- Java 文件加密传输
- java做的房产管理系统
- 基于jsp的bbs论坛 非常详细
评论
共有 条评论