如何返回 Java 中随机访问文件中一行的位置?

发布于 2024-12-22 17:11:44 字数 882 浏览 1 评论 0原文

我创建一个随机访问文件并在其中写入一些信息。 我想将随机访问文件中每一行的位置存储在一个数组中。 我将有一个索引,它指向随机访问文件的每一行。 所以我需要在索引中存储随机访问文件的行的位置。

我的程序如下

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

世俗缘 2024-12-29 17:11:44

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 of n, grabbing the result of .getFilePointer() from the RandomAccessFile object will give you the starting offset of line n+1. Line 1 starts at offset 0 and that's pretty much the only thing you have to really account for here.

剩余の解释 2024-12-29 17:11:44

另一方面,如果您真正关心的是行号,则可以使用 LineNumberReader 将写入文件中的每一行映射到给定的行号。

File file = new File("jedi.txt");

try(LineNumberReader lnr = new LineNumberReader(new FileReader(file))){
    String line = null;

    while ( (line = lnr.readLine()) != null){
        System.out.println(lnr.getLineNumber() + " " + line );
    }
}catch(IOException e){
    e.printStackTrace();
}

当然,这与 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.

File file = new File("jedi.txt");

try(LineNumberReader lnr = new LineNumberReader(new FileReader(file))){
    String line = null;

    while ( (line = lnr.readLine()) != null){
        System.out.println(lnr.getLineNumber() + " " + line );
    }
}catch(IOException e){
    e.printStackTrace();
}

Of course this has nothing to do with RandomAccessFiles, but it could be an alternative depending on what you are doing.

一念一轮回 2024-12-29 17:11:44

您可以使用临时变量来存储最后一个文件指针。如果必须反转读取行,则可以使用此变量设置读取器指针。

RandomAccessFile myReader = new RandomAccessFile(fileObject, "r");    
long current_postion = myReader.getFilePointer();
myReader.readLine();
if (line should be reversed){
   myReader.seek(current_postion); //Point to the previous line
}

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.

RandomAccessFile myReader = new RandomAccessFile(fileObject, "r");    
long current_postion = myReader.getFilePointer();
myReader.readLine();
if (line should be reversed){
   myReader.seek(current_postion); //Point to the previous line
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文