我有一个固定格式的文件。
我想根据行号访问此文件中的特定行。
例如读取第 100 行
每行的长度为 200 字节。
所以使用 RandomAccessFile 直接将光标移动到第 100 行就像:
File f = new File(myFile);
RandomAccessFile r = new RandomAccessFile(f,"rw");
r.skipBytes(200 * 99); // linesize * (lineNum - 1)
System.out.println(r.readLine());
但是,我得到的输出为空。
我在这里想念什么?
该问题继续回答我之前的问题Reaching a specific line in a file using RandomAccessFile
更新:
下面的程序完全符合我的预期:
行大小为 200 个字符。
File f = new File(myFile);
RandomAccessFile r = new RandomAccessFile(f,"rw");
r.seek(201 * (lineNumber-1)); // linesize * (lineNum - 1)
System.out.println(r.readLine());
给出行号(整个文件中的任何行号)正在打印该行。
@EJP:请解释一下!