RandomAccessFile文件实现seek标志定位
加上Buffered 缓存实现更快的读写文件
生产亲测可用性能强悍
BufferedRandomAccessFile 代码:
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 LOG_BUFF_SZ = 16; // 64K buffer
public static final int BUFF_SZ = (1 << LOG_BUFF_SZ);
static final long BUFF_MASK = ~(((long) BUFF_SZ) - 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.
版权声明:本文为kylin_tam原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。