简体   繁体   English

即使没有达到EOF,randomAccessFile.readLine()在多次使用后仍返回null。

[英]randomAccessFile.readLine() returns null after many uses even though not reaching EOF?

I have a file with 10K lines. 我有一个包含10K行的文件。

I read it in chunks of 200 lines. 我读了200行。

I have a problem that after 5600 lines (chunk 28), randomAccessFile.readLine() returns null . 我有一个问题,在5600行(第28块)之后, randomAccessFile.readLine()返回null

however, if i start reading from chunk 29 it reads another chunk and stops ( return null). 但是,如果我从块29开始读取,它将读取另一个块并停止(返回null)。

I force reading from chunk 30, and again - it reads one chunk and stops. 我强制从块30读取,然后再次-它读取一个块并停止。

this is my code: 这是我的代码:

private void addRequestsToBuffer(int fromChunkId, List<String> requests) {
    String line;
    while (requests.size() < chunkSizeInLines) {

        if ((line = readNextLine()) != null) {
            return;
        }
        int httpPosition = line.indexOf("http");
        int index = fromChunkId * chunkSizeInLines + requests.size();
        requests.add(index + ") " + line.substring(httpPosition));
    }


}

private String readNextLine() {
    String line;
    try {
        line = randomAccessFile.readLine();
        if (line == null) {
            System.out.println("randomAccessFile.readLine() returned null");
        }

    } catch (IOException ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex);
    }
    return line;
}


@Override
public List<String> getNextRequestsChunkStartingChunkId(int fromChunkId) {
    List<String> requests = new ArrayList<>();
    int linesNum = 0;
    try {
        for (int i = 0; i < fromChunkId; i++) {
            while ((linesNum < chunkSizeInLines) && (randomAccessFile.readLine()) != null) {
                linesNum++;
            }
            linesNum = 0;
        }
        addRequestsToBuffer(fromChunkId, requests);
    } catch (IOException ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex);
    }
    return requests;
}

what can cause this? 是什么原因造成的? randomAccessFile time out? randomAccessFile超时?

Each time you call getNextRequestsChunkStartingChunkId you're skipping the specified number of chunks, without "rewinding" the RandomAccessFile to the start. 每次调用getNextRequestsChunkStartingChunkId您都将跳过指定数量的块,而不会“倒回” RandomAccessFile到开头。 So for example, if you call: 因此,例如,如果您致电:

getNextRequestsChunkStartingChunkId(0);
getNextRequestsChunkStartingChunkId(1);
getNextRequestsChunkStartingChunkId(2);

you'll actually read: 您实际上会读到:

  • Chunk 0 (leaving the stream at the start of chunk 1) 0块(将数据流留在块1的开始处)
  • Chunk 2 (leaving the stream at the start of chunk 3) 第2块(将数据流留在第3块的开头)
  • Chunk 5 (leaving the stream at the start of chunk 6) 第5块(将数据流留在第6块的开头)

Options: 选项:

  • Read the chunks sequentially, without skipping anything 顺序读取块,不跳过任何内容
  • Rewind at the start of the method 在方法开始时倒带

Unfortunately you can't use seek for this, because your chunks aren't equally sized, in terms of bytes. 不幸的是,您不能为此使用seek ,因为就字节而言,块的大小不相等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM