Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java从大文件中读取数据块_Java_File_File Io_Io_Randomaccessfile - Fatal编程技术网

使用java从大文件中读取数据块

使用java从大文件中读取数据块,java,file,file-io,io,randomaccessfile,Java,File,File Io,Io,Randomaccessfile,我有一个10K实体的大文件(每行实体) 我想把它分成1K个实体来阅读 我试过: public List<String> getNextRequestsChunk() { List<String> requests = new ArrayList<>(); try { randomAccessFile.seek(currentSeekPosition); String line = null;

我有一个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;
}
当我为chunk#2重新运行此方法时,它不会给我预期的字符串
33
,而是字符串
2

chunkSize
为2行,
currentSeekPosition
=4)


如何修复此问题?

添加
currentSeekPosition=randomAccessFile.getFilePointer()
之后,而
循环

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;
}
public List getNextRequestsChunk(){
列表请求=新建ArrayList();
试一试{
randomAccessFile.seek(currentSeekPosition);
字符串行=null;
而((requests.size()

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

如果不是第10行,它会给您带来什么?请确保currentSeekPosition没有在外面的某个地方重置。请查看我的更新。我想您需要添加:currentSeekPosition+=line.length()+1;你为什么还要和seek()混在一起?只需读取数据,让文件指针自动前进。
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;
}