使用java从大文件中读取块

时间:2015-04-28 19:05:42

标签: java file file-io io randomaccessfile

我有一个包含10K实体的大文件(每行实体)

我想在1K实体的块中读取它。

我试过了:

public List<String> getNextRequestsChunk() {
    List<String> requests = new ArrayList<>();
    try {

        randomAccessFile.seek(currentSeekPosition);

        String line = null;
        while ((requests.size() < chunkSize) && (line = randomAccessFile.readLine()) != null)
        {
            currentSeekPosition += line.length();
            requests.add(line);
        }
    } catch (IOException ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex);
    }

    return requests;
}

我有这个文件:

11
22
33
..
100100

当我为块#2重新运行此方法时,它不会给我预期的字符串33但是字符串2

chunkSize为2行,currentSeekPosition = 4)

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

currentSeekPosition = randomAccessFile.getFilePointer();循环

之后添加while
public List<String> getNextRequestsChunk() {
    List<String> requests = new ArrayList<>();
    try {

        randomAccessFile.seek(currentSeekPosition);

        String line = null;
        while ((requests.size() < chunkSize) && (line = randomAccessFile.readLine()) != null)
        {
            // currentSeekPosition += line.length()+1; 
            requests.add(line);
        }
       // add this 
       currentSeekPosition = randomAccessFile.getFilePointer();
    } catch (IOException ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex);
    }

    return requests;
}

您的问题是readLine方法不计算换行符\n