我可以使用 SeekableByteChannel 从文件中读取行吗?我有位置(以字节为单位)并想阅读整行。例如,我将此方法用于 RandomAccessFile
private static String currentLine(String filepath, long currentPosition)
{
RandomAccessFile f = new RandomAccessFile(filepath, "rw");
byte b = f.readByte();
while (b != 10)
{
currentPosition -= 1;
f.seek(currentPosition);
b = f.readByte();
if (currentPosition <= 0)
{
f.seek(0);
String currentLine = f.readLine();
f.close();
return currentLine;
}
}
String line = f.readLine();
f.close();
return line;
}
我如何为 SeekableByteChannel 使用这样的东西,并且读取大量行会更快吗?