我有一个包含 10K 实体(每行实体)的大文件
我想以 1K 实体 block 的形式读取它并列出。
我已经尝试过:
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)
我该如何解决这个问题?
最佳答案
在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
。
关于java - 使用java从大文件中读取 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29927814/