我有一个包含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)
我该如何解决这个问题?
答案 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
。