为了更新RandomAccessFile 中的文件指针,您需要使用属于 RandomAccessFile 对象的read() 方法。
制作单独的阅读器不会更新它。
如果您需要使用BufferedReader,您始终可以在自己的 InputStream 实现中包装一个 RandomAccessFile,以便读取 inputStream 委托以读取 RandomAccessFile:
我以前必须这样做。不难:
public final class RandomAccessFileInputStream extends InputStream{
private final RandomAccessFile randomAccessFile;
private long bytesRead=0;
/**
* The number of bytes to read in the stream;
* or {@code null} if we should read the whole thing.
*/
private final Long length;
private final boolean ownFile;
/**
* Creates a new {@link RandomAccessFileInputStream}
* of the given file starting at the given position.
* Internally, a new {@link RandomAccessFile} is created
* and is seek'ed to the given startOffset
* before reading any bytes. The internal
* {@link RandomAccessFile} instance is managed by this
* class and will be closed when {@link #close()} is called.
* @param file the {@link File} to read.
* @param startOffset the start offset to start reading
* bytes from.
* @throws IOException if the given file does not exist
* @throws IllegalArgumentException if the startOffset is less than 0.
*/
public RandomAccessFileInputStream(File file, long startOffset) throws IOException{
assertStartOffValid(file, startOffset);
this.randomAccessFile = new RandomAccessFile(file, "r");
randomAccessFile.seek(startOffset);
this.length = null;
ownFile =true;
}
/**
* Creates a new {@link RandomAccessFileInputStream}
* of the given file starting at the given position
* but will only read the given length.
* Internally, a new {@link RandomAccessFile} is created
* and is seek'ed to the given startOffset
* before reading any bytes. The internal
* {@link RandomAccessFile} instance is managed by this
* class and will be closed when {@link #close()} is called.
* @param file the {@link File} to read.
* @param startOffset the start offset to start reading
* bytes from.
* @param length the maximum number of bytes to read from the file.
* this inputStream will only as many bytes are in the file.
* @throws IOException if the given file does not exist
* @throws IllegalArgumentException if either startOffset or length are less than 0
* or if startOffset < file.length().
*/
public RandomAccessFileInputStream(File file, long startOffset, long length) throws IOException{
assertStartOffValid(file, startOffset);
if(length < 0){
throw new IllegalArgumentException("length can not be less than 0");
}
this.randomAccessFile = new RandomAccessFile(file, "r");
randomAccessFile.seek(startOffset);
this.length = length;
ownFile =true;
}
private void assertStartOffValid(File file, long startOffset) {
if(startOffset < 0){
throw new IllegalArgumentException("start offset can not be less than 0");
}
if(file.length() < startOffset){
throw new IllegalArgumentException(
String.format("invalid startOffset %d: file is only %d bytes" ,
startOffset,
file.length()));
}
}
/**
* Creates a new RandomAccessFileInputStream that reads
* bytes from the given {@link RandomAccessFile}.
* Any external changes to the file pointer
* via {@link RandomAccessFile#seek(long)} or similar
* methods will also alter the subsequent bytes read
* by this {@link InputStream}.
* Closing the inputStream returned by this constructor
* DOES NOT close the {@link RandomAccessFile} which
* must be closed separately by the caller.
* @param file the {@link RandomAccessFile} instance
* to read as an {@link InputStream}; can not be null.
* @throws NullPointerException if file is null.
*/
public RandomAccessFileInputStream(RandomAccessFile file){
if(file ==null){
throw new NullPointerException("file can not be null");
}
this.randomAccessFile = file;
length = null;
ownFile =false;
}
@Override
public synchronized int read() throws IOException {
if(length !=null && bytesRead >=length){
return -1;
}
int value = randomAccessFile.read();
if(value !=-1){
bytesRead++;
}
return value;
}
@Override
public synchronized int read(byte[] b, int off, int len) throws IOException {
if(length != null && bytesRead >=length){
return -1;
}
final int reducedLength = computeReducedLength(len);
int numberOfBytesRead = randomAccessFile.read(b, off, reducedLength);
bytesRead+=numberOfBytesRead;
return numberOfBytesRead;
}
private int computeReducedLength(int len) {
if(length ==null){
return len;
}
return Math.min(len, (int)(length - bytesRead));
}
/**
* If this instance was creating
* using the {@link #RandomAccessFileInputStream(RandomAccessFile)}
* constructor, then this method does nothing- the RandomAccessFile
* will still be open.
* If constructed using {@link #RandomAccessFileInputStream(File, long)}
* or {@link #RandomAccessFileInputStream(File, long, long)},
* then the internal {@link RandomAccessFile} will be closed.
*/
@Override
public void close() throws IOException {
//if we created this randomaccessfile
//then its our job to close it.
if(ownFile){
randomAccessFile.close();
}
}
}
编辑
我已经尝试使用我的RandomAccessFileInputStream 运行您的代码示例,问题是即使设置了缓冲区大小,BufferedReader 出于某种原因仍在缓冲,因此每当读取底层 inputStream 时文件指针递增 8912。即使缓冲按预期工作,缓冲区将始终读取下一行,因此offset 永远不会是行尾的位置。
如果您不想缓冲数据并且不想编写自己的读取行的实现。您可以使用 DataInputStream,它有一个已弃用的 readLine() 方法。该方法已被弃用,因为它“不能正确地将字节转换为字符”,但是如果您使用的是 ASCII 字符,它应该没问题。
InputStream in = new RandomAccessFileInputStream(raf);
DataInputStream dataIn = new DataInputStream(in))
...
if ((line = dataIn.readLine()) == null)
...
按预期工作。偏移量仅更新每行的确切字节数。但是,由于它没有缓冲,因此读取文件会比较慢。