如何返回 Java 中随机访问文件中一行的位置?
我创建一个随机访问文件并在其中写入一些信息。 我想将随机访问文件中每一行的位置存储在一个数组中。 我将有一个索引,它指向随机访问文件的每一行。 所以我需要在索引中存储随机访问文件的行的位置。
我的程序如下
package randomaccessfile;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class raf {
public static void main(String[] args) throws FileNotFoundException, IOException {
File file = new File("DocumentsFile.txt");
RandomAccessFile raf = new RandomAccessFile(file, "rw");
for (int i = 0; i <= 10 ; i++) {
String sentence = "1 Info Name Surname"; //i create sentences in the random access file
raf.seek(file.length());
raf.writeBytes(sentence);
raf.writeBytes("\r\n");
}
raf.close();
}
}
对于随机访问文件中创建的每一行, 我想将它们的位置存储在数组中。
然后位置将存储在索引中。
有没有什么方法可以用来返回一行的位置 在随机访问文件中?
I create a random access file and I write some info in it.
I would like to store in an array the position of each line in the random access file.
I'm going to have an index which points to every line of the random access file.
So I need to store in my index the positions of the lines the random access file.
My program is as follows
package randomaccessfile;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class raf {
public static void main(String[] args) throws FileNotFoundException, IOException {
File file = new File("DocumentsFile.txt");
RandomAccessFile raf = new RandomAccessFile(file, "rw");
for (int i = 0; i <= 10 ; i++) {
String sentence = "1 Info Name Surname"; //i create sentences in the random access file
raf.seek(file.length());
raf.writeBytes(sentence);
raf.writeBytes("\r\n");
}
raf.close();
}
}
For each of the lines that are created in the Random access file,
I would like to store in an array their positions.
The positions then are going to be stored in the index.
Is there any method that I can use in order to return the position of a line
in the random access file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
RandomAccessFile
已经提供了您想要的一切。其一,此处不需要
seek()
:文件指针前进到每次写入操作时写入文件的字节。这意味着,在编写了
n
行之后,无论n
的值如何,都会从.getFilePointer()
中获取.getFilePointer()
的结果。 >RandomAccessFile 对象将为您提供第n+1
行的起始偏移量。第 1 行从偏移量 0 开始,这几乎是您在这里唯一需要真正考虑的事情。RandomAccessFile
already provides everything you want.For one, you don't need
seek()
here: the file pointer advances past the bytes written into the file on each write operation.This means that, after you have written line
n
, for whatever value ofn
, grabbing the result of.getFilePointer()
from theRandomAccessFile
object will give you the starting offset of linen+1
. Line 1 starts at offset 0 and that's pretty much the only thing you have to really account for here.另一方面,如果您真正关心的是行号,则可以使用 LineNumberReader 将写入文件中的每一行映射到给定的行号。
当然,这与 RandomAccessFiles 无关,但它可能是一种替代方案,具体取决于您在做什么。
On the other hand, if what you really care about are line numbers, you could use a LineNumberReader to map every line in the written file to a given line number.
Of course this has nothing to do with RandomAccessFiles, but it could be an alternative depending on what you are doing.
您可以使用临时变量来存储最后一个文件指针。如果必须反转读取行,则可以使用此变量设置读取器指针。
You can use a temporary variable to store the last FilePointer. If the readline must be reversed, you can set the reader pointer using this variable.