我有一个包含10K个实体(每行一个实体)的大文件
我想把它以1000个实体的块来列出。
我试过了:
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
当我为chunk#2重新运行此方法时,它给出的不是预期的字符串33
,而是字符串2
(chunkSize
为2行,currentSeekPosition
= 4)
我该如何解决这个问题呢?
发布于 2015-04-29 03:39:23
在while
循环后添加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;
}
您的问题是readLine
方法没有计算新行字符\n
。
https://stackoverflow.com/questions/29927814
复制相似问题