randomAccessFile.readLine() returns 即使未达到 EOF,多次使用后仍为空?
randomAccessFile.readLine() returns null after many uses even though not reaching EOF?
我有一个 10K 行的文件。
我一共读了 200 行。
我有一个问题,在 5600 行(第 28 块)之后,randomAccessFile.readLine()
returns null
.
但是,如果我从块 29 开始读取,它会读取另一个块并停止(return null)。
我强制从块 30 读取,然后再次读取 - 它读取一个块并停止。
这是我的代码:
private void addRequestsToBuffer(int fromChunkId, List<String> requests) {
String line;
while (requests.size() < chunkSizeInLines) {
if ((line = readNextLine()) != null) {
return;
}
int httpPosition = line.indexOf("http");
int index = fromChunkId * chunkSizeInLines + requests.size();
requests.add(index + ") " + line.substring(httpPosition));
}
}
private String readNextLine() {
String line;
try {
line = randomAccessFile.readLine();
if (line == null) {
System.out.println("randomAccessFile.readLine() returned null");
}
} catch (IOException ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
return line;
}
@Override
public List<String> getNextRequestsChunkStartingChunkId(int fromChunkId) {
List<String> requests = new ArrayList<>();
int linesNum = 0;
try {
for (int i = 0; i < fromChunkId; i++) {
while ((linesNum < chunkSizeInLines) && (randomAccessFile.readLine()) != null) {
linesNum++;
}
linesNum = 0;
}
addRequestsToBuffer(fromChunkId, requests);
} catch (IOException ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
return requests;
}
这是什么原因造成的? randomAccessFile
超时?
每次您调用 getNextRequestsChunkStartingChunkId
时,您都会跳过指定数量的块,"rewinding" 和 RandomAccessFile
都没有开始。例如,如果您调用:
getNextRequestsChunkStartingChunkId(0);
getNextRequestsChunkStartingChunkId(1);
getNextRequestsChunkStartingChunkId(2);
你实际上会读到:
- 块 0(在块 1 的开头留下流)
- 块 2(在块 3 的开头留下流)
- 块 5(在块 6 的开头离开流)
选项:
- 按顺序读取块,不跳过任何内容
- 在方法开始时倒带
很遗憾,您不能为此使用 seek
,因为您的数据块大小不均(以字节为单位)。
我有一个 10K 行的文件。
我一共读了 200 行。
我有一个问题,在 5600 行(第 28 块)之后,randomAccessFile.readLine()
returns null
.
但是,如果我从块 29 开始读取,它会读取另一个块并停止(return null)。
我强制从块 30 读取,然后再次读取 - 它读取一个块并停止。
这是我的代码:
private void addRequestsToBuffer(int fromChunkId, List<String> requests) {
String line;
while (requests.size() < chunkSizeInLines) {
if ((line = readNextLine()) != null) {
return;
}
int httpPosition = line.indexOf("http");
int index = fromChunkId * chunkSizeInLines + requests.size();
requests.add(index + ") " + line.substring(httpPosition));
}
}
private String readNextLine() {
String line;
try {
line = randomAccessFile.readLine();
if (line == null) {
System.out.println("randomAccessFile.readLine() returned null");
}
} catch (IOException ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
return line;
}
@Override
public List<String> getNextRequestsChunkStartingChunkId(int fromChunkId) {
List<String> requests = new ArrayList<>();
int linesNum = 0;
try {
for (int i = 0; i < fromChunkId; i++) {
while ((linesNum < chunkSizeInLines) && (randomAccessFile.readLine()) != null) {
linesNum++;
}
linesNum = 0;
}
addRequestsToBuffer(fromChunkId, requests);
} catch (IOException ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
return requests;
}
这是什么原因造成的? randomAccessFile
超时?
每次您调用 getNextRequestsChunkStartingChunkId
时,您都会跳过指定数量的块,"rewinding" 和 RandomAccessFile
都没有开始。例如,如果您调用:
getNextRequestsChunkStartingChunkId(0);
getNextRequestsChunkStartingChunkId(1);
getNextRequestsChunkStartingChunkId(2);
你实际上会读到:
- 块 0(在块 1 的开头留下流)
- 块 2(在块 3 的开头留下流)
- 块 5(在块 6 的开头离开流)
选项:
- 按顺序读取块,不跳过任何内容
- 在方法开始时倒带
很遗憾,您不能为此使用 seek
,因为您的数据块大小不均(以字节为单位)。