即使缓冲区大小很小,BufferedReader的readLine也不会更改文件指针

时间:2015-01-28 19:09:16

标签: java android bufferedreader randomaccessfile

我的应用程序逐行读取文本文件并记录每行的偏移量,直到文件末尾。仅在首次执行readLine时偏移更改。之后它不会再改变了。我使用bufferSize从10到16384进行了测试。我的代码出了什么问题?我使用RandomAccessFile而不是FileInputStream,因为当文件很大时,seek()比skip()更快。

String line;        
long offset;

RandomAccessFile raf = new RandomAccessFile("data.txt", "r");
FileInputStream is = new FileInputStream(raf.getFD());
InputStreamReader isr = new InputStreamReader(is, encoding);
BufferedReader br = new BufferedReader(isr, bufferSize);

while (true) {
    offset = raf.getFilePointer(); // offset remains the same after 1st readLine. why?
    if ((line = br.readLine()) == null) // line has correct value.
        return;
    ………………………………
}

1 个答案:

答案 0 :(得分:2)

为了更新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) 
  ...

按预期工作。偏移量仅更新每行的确切字节数。但是,由于它没有缓冲,因此读取文件的速度会慢一些。